Last active
November 30, 2019 21:08
-
-
Save fauxsaurus/2c00f5080728c0913db12d4ed99cd520 to your computer and use it in GitHub Desktop.
Run array map functions sequentially & asynchronously.
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 asyncMap=(arr,cb)=>arr.reduce(async (arr,...args)=>[...(await arr),await cb(...args)],[]) | |
| const wait=async ms=> | |
| new Promise(res=> | |
| setTimeout(()=> | |
| { | |
| const newVal=ms+"ms"; | |
| console.log(newVal); | |
| res(newVal); | |
| }, ms) | |
| ) | |
| asyncMap([2000,500,1000,5000],wait).then(console.log) | |
| /*console: | |
| Promise {<pending>} | |
| 2000ms | |
| 500ms | |
| 1000ms | |
| 5000ms | |
| ['2000ms','500ms','1000ms','5000ms'] | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment