Created
May 30, 2018 12:59
-
-
Save giordanocardillo/aa092f99268db560aef8215e60d69761 to your computer and use it in GitHub Desktop.
JS ForEach with async wait
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 asyncFn = (param) => { | |
| return new Promise((resolve, reject) => { | |
| setTimeout(()=>{ | |
| console.info(param) | |
| resolve(param) | |
| }, 1e3) | |
| }) | |
| } | |
| console.info('before'); | |
| [1, 2, 3].reduce((promiseChain, arrayItem) => | |
| promiseChain.then(() => asyncFn(arrayItem)), Promise.resolve()).then(()=>{ | |
| console.info('after') | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment