promises-aplus / constructor-spec

Discussion and drafts of a possible spec for creating and resolving promises
10 stars 4 forks source link

Asynchronous #14

Open ForbesLindesay opened 11 years ago

ForbesLindesay commented 11 years ago

We've had the discussion and accepted that Promises/A+ promises are always asynchronous for then but what about for calls to resolve

let {promise, resolve} = defer();
console.log(1);
promise.then(() => console.log(4));
console.log(2);
resolve(null);
console.log(3);

If resolve is allowed to be synchronous, this could result in:

1
2
4
3

But if it must be asynchronous then it must result in

1
2
3
4

Because of the restrictions of Promises/A+ it can never result in

1
4
2
3

So what's it to be? Do we force asynchronous behavior?

domenic commented 11 years ago

Very interesting, thanks for bringing this up! My first instinct is that synchronicity would be bizarre. But, not based on anything concerete yet.

Let's try this similar scenario:

let {promise, resolve} = defer();
console.log(1);
resolve(null);
promise.then(() => console.log(4));
console.log(2);
console.log(3);

The only thing this can be, by Promises/A+, is

1
2
3
4

To me it seems weird that the output would change depending on whether you hooked up the then before or after the resolve.

ForbesLindesay commented 11 years ago

Another question is how that plays with 'synchronous inspection'.