Last active
March 5, 2020 13:45
-
-
Save jwulf/668e0097484c2ad7e3e513c4577ec199 to your computer and use it in GitHub Desktop.
A code example from the article https://www.joshwulf.com/blog/2020/03/array-async-failure
This file contains hidden or 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
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