Last active
March 5, 2020 03:10
-
-
Save jwulf/6e163088f80b3b59644ad7c1abbc81a6 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 myAsyncFunction(argument) { | |
const success = async res => ({ success: await res }) // Promise<{success: res}> | |
const error = async err => ({ error: await err }) // Promise<{error: e}> | |
const arrayTasks = [ | |
{ | |
run: async () => getDataAsync() | |
} | |
}, | |
{ | |
run: async () => getDataAsync() | |
} | |
] | |
const runWithResult = task => task.run().then(success).catch(error) | |
const outcomes = await Promise.all(arrayTasks.map(runWithResult)) | |
console.log(outcomes) // array of {error: e} | {success: res} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment