Skip to content

Instantly share code, notes, and snippets.

@isRuslan
Created January 31, 2019 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isRuslan/0e98cd3735b631420d756649efdfb5dc to your computer and use it in GitHub Desktop.
Save isRuslan/0e98cd3735b631420d756649efdfb5dc to your computer and use it in GitHub Desktop.
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