Promises: when vs then

781 views
Skip to first unread message

AJ ONeal

unread,
Nov 4, 2010, 12:36:16 AM11/4/10
to comm...@googlegroups.com
"when" means to me "as soon as" whereas "then" means "next" and "after"

I argue that "when" is better associated with a promise than "then".
"then" would be better associated with a sequence or chain.


Let's say we have three asynchronous functions fnX fnY fnZ

Consider promise.when(fnX).when(fnY).when(fnZ)

I would expect that each executes after the promise is fulfilled,
but they may finish out of order (even though I would expect
that in the internal queue than are executed in order).


Consider promise.then(fnX).then(fnY).then(fnZ)

I would expect that each executes after the previous is fulfilled,
and in order.
fnZ must finish after fnY, which must finish after fnX



AJ ONeal

Nathan Stott

unread,
Nov 4, 2010, 1:08:13 AM11/4/10
to comm...@googlegroups.com
I agree that when makes more sense. I prefer the when(promise, fn,
fn) paradigm though.

> --
> You received this message because you are subscribed to the Google Groups
> "CommonJS" group.
> To post to this group, send email to comm...@googlegroups.com.
> To unsubscribe from this group, send email to
> commonjs+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/commonjs?hl=en.
>

Kris Zyp

unread,
Nov 4, 2010, 12:03:48 PM11/4/10
to CommonJS
Yes, that is the right order, chained promises execute in order. You
would register then() repeatedly on the initial promise if order
didn't matter.

The English words when vs then indicate opposite order.
"do operation a *when* operation b: indicates operation b comes first.
"do operation a *then* operation b: indicates operation a comes first.
Using "when" for a promise method contradicts English, unless you want
the callback to execute first, which is impossible.

Using when as a static function that acts on the next argument does
make sense in English.

The preference between using a static function vs a promise method and
the extra guarantees that the static function can provide vs the
convenience of a promise method is entirely different discussion (and
we certainly have had it in quite some depth :) ).
Kris
Reply all
Reply to author
Forward
0 new messages