snippetjavascriptTip
Run JavaScript promises in series
Viewed 0 times
javascriptseriesrunpromises
Problem
JavaScript promises are asynchronous, meaning that they are executed in parallel. This is great for performance, but sometimes you need to execute promises one after another (sequentially). This can be easily accomplished by chaining promises together, using
Array.prototype.reduce(). Each promise in the chain returns the next promise when resolved, using Promise.prototype.then().Solution
const runPromisesInSeries = ps =>
ps.reduce((p, next) => p.then(next), Promise.resolve());
const delay = d => new Promise(r => setTimeout(r, d));
runPromisesInSeries([() => delay(1000), () => delay(2000)]);
// Executes each promise sequentially, taking a total of 3 seconds to completeCode Snippets
const runPromisesInSeries = ps =>
ps.reduce((p, next) => p.then(next), Promise.resolve());
const delay = d => new Promise(r => setTimeout(r, d));
runPromisesInSeries([() => delay(1000), () => delay(2000)]);
// Executes each promise sequentially, taking a total of 3 seconds to completeContext
From 30-seconds-of-code: run-promises-in-series
Revisions (0)
No revisions yet.