GitHub 6219★

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
});
Follow me on and to get more useful contents.