Created
October 10, 2017 22:54
-
-
Save C-Rodg/f6f44981e667082b3e3ff95148f2b001 to your computer and use it in GitHub Desktop.
Example of doing a typically async action as synchronous with reduce() or async/await.
This file contains hidden or 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
const itemIds = [1,2,3,4,5,6]; | |
// Using reduce | |
itemIds.reduce((promise, id) => { | |
return promise.then(_ => api.deleteItem(id)); | |
}, Promise.resolve()); | |
// Using Async/Await | |
itemIds.forEach(async (item) => { | |
await api.deleteItem(item); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment