[Home] >  Misc >  Run Promises in sequence

Run Promises in sequence

JavaScript version

// `promises` is an array of `Promise`
const run = (promises) => promises.reduce((p, c) => p.then((rp) => c.then((rc) => [...rp, rc])), Promise.resolve([]));

TypeScript version

const run = (promises: Promise<any>[]): Promise<any> => promises.reduce((p, c) => p.then((rp) => c.then((rc) => [...rp, rc])), Promise.resolve([]));

Examples

run(promises).then((results) => {
// `results` is an array of promise results in the same order
});