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

Promise progress handlers firing in wrong order on a settled promise #82

Closed
simonzack opened this issue Jan 28, 2014 · 3 comments
Closed

Comments

@simonzack
Copy link

The following prints 2 then 1. Shouldn't it be the other way around?

var Promise = require('bluebird');

function main(){
    var resolver = Promise.defer();
    resolver.progress(1);
    resolver.resolve(2);
    var promise = resolver.promise;

    promise.then(function(res){
        console.log(res);
    }).progressed(function(res){
        console.log(res);
    });
}

main();
@gaearon
Copy link

gaearon commented Jan 28, 2014

Why should it?

I don't know if you meant it, but you're listening to progressed on a promise returned by then, not on the original promise.

I'm not sure if spec covers it, but in my view it makes sense for second promise's progressed to execute later then first promises' then.

This code prints 1 2 as you intended:

var Promise = require('bluebird');

function main(){
    var resolver = Promise.defer();
    resolver.progress(1);
    resolver.resolve(2);
    var promise = resolver.promise;

    promise.then(function(res){
        console.log(res);
    });

    promise.progressed(function(res){
        console.log(res);
    });
}

main();

I'm not sure if it's right though: this draft specifically says “onProgress is never called once a promise has already been fulfilled or rejected.”

If this is the case, maybe this code should only print 2 since promise was already resolved by the time we subscribed to its progress. (It does print 2 if we call then and progressed in next tick.)

@simonzack
Copy link
Author

Yes I think that makes sense too now, I interpreted this as having both listeners being on the same promise. Thanks for explaining this.

@gaearon
Copy link

gaearon commented Jan 28, 2014

@simonzack No problem. then always returns a new promise. See this iconic post on how jQuery stubbornly insisted on getting it wrong.

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