Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a issue by promise-chaining? #32

Closed
deleonio opened this issue Aug 18, 2016 · 4 comments
Closed

Is there a issue by promise-chaining? #32

deleonio opened this issue Aug 18, 2016 · 4 comments

Comments

@deleonio
Copy link

deleonio commented Aug 18, 2016

Hello,

I try to chain promises correct. But there is something magic.

fdescribe('Test then by promise-chaining', function () {

  function httpPromise(counter, length) {
    return new Promise(function (resolve, reject) {
      if (counter >= length) {
        resolve(counter);
      } else {
        reject(counter += 1);
      }
    });
  }

  function thenFirst() {
    return new Promise(function (resolve) {
      var length = 2;
      httpPromise(0, length)
        .then(function (counter) {
          resolve(counter);
        })
        .catch(function (counter) {
          console.log('1', counter);
          return httpPromise(counter, length)
            .catch(function (counter) {
              console.log('2', counter);
              return httpPromise(counter, length)
                .catch(function (counter) {
                  console.log('3', counter);
                  return httpPromise(counter, length);
                });
            });
        });
    });
  }

  function thenLast() {
    return new Promise(function (resolve) {
      var length = 2;
      httpPromise(0, length)
        .catch(function (counter) {
          console.log('1', counter);
          return httpPromise(counter, length)
            .catch(function (counter) {
              console.log('2', counter);
              return httpPromise(counter, length)
                .catch(function (counter) {
                  console.log('3', counter);
                  return httpPromise(counter, length);
                });
            });
        })
        .then(function (counter) {
          resolve(counter);
        });
    });
  }

  it('then first (failed)', function (done) {
    thenFirst()
      .then(function (result) {
        console.log(result);
        done();
      });
  });

  it('then last (completed)', function (done) {
    thenLast()
      .then(function (result) {
        console.log(result);
        done();
      });
  });

});
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
@deleonio deleonio changed the title Is there a issue by primise-chaining? Is there a issue by promise-chaining? Aug 18, 2016
@taylorhakes
Copy link
Owner

taylorhakes commented Aug 20, 2016

@martinoppitz I am having a little trouble following your test. What is the expected behavior for each test?

@deleonio
Copy link
Author

If the then comes first, the test failed. Why?

@taylorhakes
Copy link
Owner

Promise chains execute from top to bottom. Once a part of the chain is skipped, it doesn't go back. In your first example, the first httpPromise gets rejected, so the first then is skipped (because rejected is an error) and it goes to the first catch (after the then). Eventually the catch returns a resolved Promise, but the firstthen has already been skipped. It looks for another then, but there isn't one. The Promise never gets resolved because the call resolve was in the first then (that was skipped).

I may be missing something, but does that make sense? Does the first test pass with some other library? I tried with native Promise and it fails there too.

@deleonio
Copy link
Author

Thanks for the explanation. I think I now understand it. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants