In my example, each iteration of the loop waits on the previous promise to complete — since this is a serial map, where we’d expect the function to be run once per element, in order, rather than in parallel.

If you just run:

for (let item of collection) {
promise = fn(item).then(result => results.push(result));
}

It’s not waiting on the previous promise for each iteration, which essentially means it’s going to call the function multiple times in parallel for each item in the array.

That’s fine if you want a parallel map() — but not if you want things to run in series.

works for PayPal, as a lead engineer in Checkout. Opinions expressed herein belong to him and not his employer. daniel@bluesuncorp.co.uk

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store