Skip to content

Instantly share code, notes, and snippets.

@mdrideout
Last active March 26, 2018 17:05
Show Gist options
  • Save mdrideout/3bc60d4e62cc53edf2e52d7303a14ef2 to your computer and use it in GitHub Desktop.
Save mdrideout/3bc60d4e62cc53edf2e52d7303a14ef2 to your computer and use it in GitHub Desktop.
Demonstration of synchronously executed functions using reduce and promises
let myArray = ["beans", "soup", "peanuts", "artichokes"];
myArray.reduce((promise, item) => {
return promise.then(() => {
return itemsPromise(item);
});
}, Promise.resolve()).then(() => {
console.log("ALL DONE");
})
let itemsPromise = (item) => {
console.log("Item: ", item);
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, 2000);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment