Skip to content

Instantly share code, notes, and snippets.

@lauri-kaariainen
Last active March 20, 2021 20:13
Show Gist options
  • Save lauri-kaariainen/4b3e131c868a381d9976e7ea8ea37dfe to your computer and use it in GitHub Desktop.
Save lauri-kaariainen/4b3e131c868a381d9976e7ea8ea37dfe to your computer and use it in GitHub Desktop.
Promise.all but sequentally!
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