Tuesday 25 July 2017

How to Use Async/Await to Wait for forEach Loop to Complete Before Moving On

I've searched for solutions, but I'm not quite getting it. I want to process some calls thru a _.forEach() loop, and not continue until after the loop is complete... or, rather... until all the code called from within the loop is complete... but I'm not sure where to put the asyncs and awaits to accomplish this.For example: let sources = { ...a dictionary... } async function getAllData() { _.forEach( sources, function( source ) { processThisSource( source ) // I want all these processes to complete before moving on } } getAllData().then( () => {} ) This doesn't seem to work. Can someone point me in the right direction?Thank you!

Submitted July 25, 2017 at 11:23PM by cklester

No comments:

Post a Comment