Skip to content

Instantly share code, notes, and snippets.

@Rwin90
Created March 25, 2018 08:53
Show Gist options
  • Save Rwin90/4e322bcec691d629f886e8b99a7ee56b to your computer and use it in GitHub Desktop.
Save Rwin90/4e322bcec691d629f886e8b99a7ee56b to your computer and use it in GitHub Desktop.
a generic function that sequencialy run a function on a promiss array
const waitForEach = (processFunc , [head, ...tail]) =>
!head
? Promise.resolve()
: processFunc(head).then(waitForEach(processFunc , tail));
// example of usage
function proceessAllUser() {
const sql = 'SELECT ID FROME users';
return db.query(sql , [])
.then( users => waitForEach(user => processUser(user.id) , users))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment