3 ms·
I would argue that `Promise.all` is not the equivalent of `forEach` because Promise.all will execute everything immediately, maybe all will fail, maybe 1 will f
by greggman 11y ago
I would argue that `Promise.all` is not the equivalent of `forEach` because Promise.all will execute everything immediately, maybe all will fail, maybe 1 will fail, eventually you'll have your then or your catch called but 100 of your actions will have an attempt to be executed.
Compare that to forEach, if one fails and you throw the rest don't get executed.
I suppose if whatever you're doing for each thing is async then they are equivalent equivalent but in general forEach has different sementics than Promise.all
- MasterScrat 11y agoNow I wonder what would be the good way to make a forEach that actually returns when all elements have been executed?
- dcherman 11y agoBluebird has this built in. For a quick userscript where I needed something similar, I created this which works ( warning: no unit tests, but it works for me ) https://gist.github.com/dcherman/4dfba0d72c008ee5b59d https://gist.github.com/dcherman/4dfba0d72c008ee5b59d It's a reduce function, but it could work as a forEach as well.
- inglor 11y agoBluebird has a `Promise.each` which does this. Also, you can use `thenable chaining` - make an initial promise and then chain it on each iteration - it becomes very similar to a for loop. I can add an example if you'd like.
- nolanl 11y agoYou're right; I'm playing a bit fast and loose with the definition of "equivalent." However, I think it's what most people want when they reach for the forEach(). In that "Promise protips" gist (https://gist.github.com/nolanlawson/6ce81186421d2fa109a4 https://gist.github.com/nolanlawson/6ce81186421d2fa109a4), I have an example of something closer to what you describe: the sequentialize() function will execute one promise after the other, and fail as soon as a single one fails. However, if you need to actually run multiple promises in parallel, but cancel them as soon as a single one fails, then you might want something more advanced than promises, like RactiveJS (https://github.com/ractivejs/ractive https://github.com/ractivejs/ractive) or RxJS (https://github.com/Reactive-Extensions/RxJS https://github.com/Reactive-Extensions/RxJS).
- robmcm 11y agoIt would also really help if you were verbose in the naming of your methods as to weather they were async or not. At first I didn't realise db.remove() was async (sorry perhaps that's my fault). Also on your timing diagrams it may be worth indicating that methods don't start executing at the exact same time due to the single thread: doSomething().then(function () { doSomethingElse(); }).then(finalHandler); doSomething |-----------------| doSomethingElse(undefined) ..................|------------------| finalHandler(undefined) ....................|------------------| It's my understanding that the finally handler can not execute before the body of doSomethingElse has run. (Although I am unsure of a pretty/clear way to add this to a diagram ;) )