Created
August 21, 2018 12:43
-
-
Save sramam/2e18ef9224c81721aa697c10a496f8a9 to your computer and use it in GitHub Desktop.
p-map - testing concurrency when errors happen
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 pmap = require('p-map'); | |
const delay = (n) => new Promise(res => setTimeout(res, n)); | |
const promises = [ | |
new Promise(async (res) => { | |
await delay(20); | |
res('a') | |
}), | |
new Promise(async (res) => { | |
await delay(10); | |
res('b') | |
}), | |
new Promise(async (res, rej) => { | |
await delay(11); | |
rej('c') | |
}), | |
new Promise(async (res, rej) => { | |
await delay(19); | |
res('d') | |
}), | |
new Promise(async (res) => { | |
await delay(21); | |
res('e') | |
}) | |
]; | |
process.on('unhandledRejection', error => { | |
// Will print "unhandledRejection err is not defined" | |
console.log('unhandledRejection', error); | |
}); | |
result = [] | |
pmap(promises, x => result.push(x), { concurrency: 2}).then(x => console.log(result)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment