Last active
August 29, 2015 14:24
-
-
Save mgtitimoli/d7a9b94ab64a21f4cb57 to your computer and use it in GitHub Desktop.
Promise based parallel task executor
This file contains 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
"use strict"; | |
function parallel(tasks, ...args) { | |
let error; | |
let results = []; | |
for (let curTask of tasks) { | |
try { | |
results.push(curTask(...args)); | |
} | |
catch(e) { | |
error = e; | |
break; | |
} | |
} | |
if (error) { | |
return Promise.reject(error); | |
} | |
return Promise.all(results); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment