Last active
March 26, 2018 17:05
-
-
Save mdrideout/3bc60d4e62cc53edf2e52d7303a14ef2 to your computer and use it in GitHub Desktop.
Demonstration of synchronously executed functions using reduce and promises
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
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