feross / feross/run-parallel

Promise.all ?

Open
#24 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
385
Forks
16
PR merge metrics
No merged PRs in 30d

Description

Hi,

if you want users to ship small bundle sizes, would it be an idea to reference [Promise.all()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) in the docs ?

Just for fun I recreated run-parallel with Promise.all() and ended up with:

```javascript
function runParallel (tasks, cb) {
const safecb = cb || (() => {})
if (Array.isArray(tasks)) {
// array
Promise.all(tasks.map(item => new Promise(
(resolve, reject) => item((err, result) => err ? reject(err) : resolve(result))
)))
.then(results => safecb(null, results))
.catch(err => safecb(err, null))
} else {
// object
Promise.all(Object.keys(tasks).map(item => new Promise(
(resolve, reject) => tasks[item]((err, result) => err ? reject(err) : resolve([item, result]))
)))
.then(results => safecb(null, Object.fromEntries(results)))
.catch(err => safecb(err, null))
}
}
```

This meets all tests except for one:

```javascript
test('functions that return errors (object) w/ partial results', function (t) {
t.plan(4)

const tasks = {
one: function (cb) {
t.pass('cb 1')
cb(null, 1)
},
two: function (cb) {
setTimeout(function () {
t.pass('cb 2')
cb(new Error('oops'))
}, 100)
}
}

parallel(tasks, function (err, results) {
t.ok(err instanceof Error)
t.deepEqual(results, { one: 1, two: undefined })
})
})
```
However that use case might be better off with [Promise.allSettled](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled)

Kind regards,
Hans

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.