Skip to content

Instantly share code, notes, and snippets.

@sramam
Created August 21, 2018 12:43
Show Gist options
  • Save sramam/2e18ef9224c81721aa697c10a496f8a9 to your computer and use it in GitHub Desktop.
Save sramam/2e18ef9224c81721aa697c10a496f8a9 to your computer and use it in GitHub Desktop.
p-map - testing concurrency when errors happen
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