Skip to content

Commit

Permalink
Replace promise-control-flow with a custom implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
queicherius committed Oct 4, 2018
1 parent 6b461e7 commit eb700de
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
"debounce": "^1.2.0",
"fast-get": "^1.0.3",
"idb-keyval": "^3.1.0",
"lets-fetch": "^2.1.2",
"promise-control-flow": "^1.2.5",
"lets-fetch": "^2.1.3",
"querystringify": "^2.0.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/client.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fetch = require('lets-fetch')
const flow = require('promise-control-flow')
const nullCache = require('./cache/null')
const endpoints = require('./endpoints')
const flow = require('./flow')

module.exports = class Client {
constructor () {
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/account-blob.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const flow = require('promise-control-flow')
const _get = require('fast-get')
const flow = require('../flow.js')
const api = require('../index.js')

function blob (parent) {
Expand Down
19 changes: 19 additions & 0 deletions src/flow.js
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 }
28 changes: 28 additions & 0 deletions tests/flow.spec.js
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'
])
})
})

0 comments on commit eb700de

Please sign in to comment.