Created
January 31, 2019 09:07
-
-
Save isRuslan/0e98cd3735b631420d756649efdfb5dc to your computer and use it in GitHub Desktop.
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
const timeout = (ms) => new Promise(resolve => setTimeout(resolve, ms)) | |
function getJob(ms, id) { | |
return async function () { | |
return job(ms, id) | |
} | |
} | |
async function job(ms, id) { | |
await timeout(ms) | |
if (Math.random() > 0.5) { | |
throw new Error(`Error ${id}`) | |
} | |
console.log(`Done ${id}`); | |
} | |
async function main() { | |
const tasks = [getJob(1000, 'a'), getJob(2000, 'b'), getJob(1000, 'c')]; | |
for (let task of tasks) { | |
try { | |
await task() | |
} catch (e) { | |
console.log(e) | |
} | |
} | |
console.log('Hey') | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment