💾Promise.inSeries()
Last updated
Was this helpful?
Last updated
Was this helpful?
Was this helpful?
JS ⟩ async ⟩ Promise ⟩ chaining ⟩ Promise.inSeries()
💾 replit:Promise in series
// 🔸 Promise.inSeries()
Promise.inSeries = function(promises) {
// store results from promises
const results = [];
// ⭐ start with a "trivial" Promise
let p = Promise
💈範例:
// test 1
Promise.inSeries([
Promise.resolve(1),
Promise.reject('error in promise #2'),
Promise.resolve(3),
]).then(results =>
console.log(results)
).catch(error => {
console.log(error) // ⛔ rejected: "error in promise #2"
});
// test 2
Promise.inSeries([
new Promise(resolve => setTimeout(resolve, 2000, 1)),
new Promise(resolve => setTimeout(resolve, 1000, 2)),
Promise.resolve(3),
]).then(results =>
console.log(results) // ✅ fulfilled: [ 1, 2, 3 ]
).catch(error => {
console.log(error)
});