Last active
March 20, 2021 20:13
-
-
Save lauri-kaariainen/4b3e131c868a381d9976e7ea8ea37dfe to your computer and use it in GitHub Desktop.
Promise.all but sequentally!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var list = [ | |
"someJsonUrl", | |
"someJsonUrl", | |
"someJsonUrl" | |
].map((url) => fetch.bind(null, url)); | |
function sequentialPromise(promiseFuncList) { | |
return new Promise((resolve, reject) => { | |
const resultsList = []; | |
promiseFuncList.reduce((accPromise, nextPromiseFunc, i, arr) => { | |
return accPromise.then((res) => { | |
if (res && res.json) | |
res.json().then((e) => { | |
resultsList.push(e); | |
if (i === arr.length - 1) { | |
resolve(resultsList); | |
} | |
}); | |
if (i < arr.length - 1) return arr[i + 1](); | |
}); | |
}, promiseFuncList[0]()); | |
}); | |
} | |
sequentialPromise(list).then((res) => console.log(res)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment