-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace
promise-control-flow
with a custom implementation
- Loading branch information
1 parent
6b461e7
commit eb700de
Showing
5 changed files
with
50 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
async function parallel (promises) { | ||
const results = await Promise.all( | ||
Object.values(promises).map(func => func()) | ||
) | ||
|
||
// If the initial structure was an array, just return the array of results | ||
if (Array.isArray(promises)) { | ||
return results | ||
} | ||
|
||
// If the initial structure was an object, rebuild an object with the results | ||
const keys = Object.keys(promises) | ||
return results.reduce((object, resultPart, index) => { | ||
object[keys[index]] = resultPart | ||
return object | ||
}, {}) | ||
} | ||
|
||
module.exports = { parallel } |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* eslint-env jest */ | ||
const flow = require('../src/flow') | ||
|
||
describe('flow', () => { | ||
it('handles parallel promises with an object', async () => { | ||
const result = await flow.parallel({ | ||
a: () => Promise.resolve('AAA'), | ||
b: () => Promise.resolve('BBB') | ||
}) | ||
|
||
expect(result).toEqual({ | ||
a: 'AAA', | ||
b: 'BBB' | ||
}) | ||
}) | ||
|
||
it('handles parallel promises with an array', async () => { | ||
const result = await flow.parallel([ | ||
() => Promise.resolve('AAA'), | ||
() => Promise.resolve('BBB') | ||
]) | ||
|
||
expect(result).toEqual([ | ||
'AAA', | ||
'BBB' | ||
]) | ||
}) | ||
}) |