Skip to content

Instantly share code, notes, and snippets.

@jwulf
Last active March 5, 2020 13:45
Show Gist options
  • Save jwulf/668e0097484c2ad7e3e513c4577ec199 to your computer and use it in GitHub Desktop.
Save jwulf/668e0097484c2ad7e3e513c4577ec199 to your computer and use it in GitHub Desktop.
async function executeAsyncTasks(tasks: AsyncTask<any>[]) {
const success = async res => ({ success: await res }) // Promise<{success: res}>
const error = async err => ({ error: await err }) // Promise<{error: e}>
const runWithResult = task => task.run()
.then(result => {
success(result)
task._success(result)
})
.catch(e => {
error(e)
task._error(e)
})
const outcomes = await Promise.all(tasks.map(runWithResult))
const outcomesFlat = outcomes.reduce((acc, o) => o.error ?
{ error: [...acc.error, o.error], success: acc.success } :
{ error: acc.error, success: [...acc.success, o.success] },
{ error: [], success: [] })
return outcomesFlat
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment