5 ms·
Now I wonder what would be the good way to make a forEach that actually returns when all elements have been executed?
by MasterScrat 11y ago
Now 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.