Skip to content

Instantly share code, notes, and snippets.

@fauxsaurus
Last active November 30, 2019 21:08
Show Gist options
  • Select an option

  • Save fauxsaurus/2c00f5080728c0913db12d4ed99cd520 to your computer and use it in GitHub Desktop.

Select an option

Save fauxsaurus/2c00f5080728c0913db12d4ed99cd520 to your computer and use it in GitHub Desktop.
Run array map functions sequentially & asynchronously.
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