Re:JavaScript: It’s Not Just for Browsers Any More
by scotty79 on Saturday March 06, 2010 @06:07AM
(#1171403)
> Finally, to answer the grandparent question
I don't event have a kid! ;-)
Thank you, that was the thing I was looking for. My question now: What happens if one of the promises fails? Is a promise group disposed then? Solution offered by Periodic would wait forever.
Re:JavaScript: It’s Not Just for Browsers Any More
by simonw on Saturday March 06, 2010 @09:25AM
(#1171562)
It depends on the promise library implementation - most will let you specify an "errback" (like a callback, but called if there is an error) and will trigger that if any of their children fail. Alternatively, some may let you specify a timeout and call your errback if that time elapses without all of the children finishing.
All parallelism is cooperative in javascript, meaning that you don't have to worry about control flow switching out from under you at arbitrary points, just when you explicitly yield it by returning from an event handler or from the top level of your program.
Finally, to answer the grandparent question, the easiest answer is a promise group. A promise group is basically a promise that emits a success event once all of its child promises have emitted success events. There are a few implementations floating around, though it's just a handful of lines of code to write and makes for a good exercise.
So assuming that A and B are rewritten to return promises (or wrapped, I believe that isaacs has a library which will wrap functions which take a callback to return a promise instead), you can do the following: