Promise/Future: asynchrony in 'then'

Claus Reinke claus.reinke at talk21.com
Tue Apr 30 09:43:58 PDT 2013


The promises-aplus spec has a note that confuses me

    https://github.com/promises-aplus/promises-spec#notes

    1. In practical terms, an implementation must use a mechanism such 
    as setTimeout, setImmediate, or process.nextTick to ensure that 
    onFulfilled and onRejected are not invoked in the same turn of the 
    event loop as the call to then to which they are passed.

I have not yet been able to decide whether DOMFuture has a
similar provision, or how this note is meant to be interpreted.

The aspect that worries me is that this note is attached not to the
creation of promises but to the definition of 'then'. Is that because
of the implicit return lifting (if 'then' callbacks do not return promises,
wrap the return in a new promise), or is there something else going on?

As long as the 'then' callbacks return Promises, the idea of resolved 
Promise creation as left and right identity of 'then'

    Promise.of(value).then(cb) = cb(value)
    promise.then(Promise.of) = promise

would seem to require no additional delays introduced by 'then' 
(promise creation decides semantics/delays, 'then' only passes on 
intermediate results).

Could someone please clear up this aspect? How is that note meant
to be interpreted, and do other Promise/Future specs have similar
provisions?

Claus

PS. Prompted by this blog post:
    http://thanpol.as/javascript/promises-a-performance-hits-you-should-be-aware-of/ 


More information about the es-discuss mailing list