-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Require Node.js 8, add TypeScript definition, update dependencies (#3)
- Loading branch information
1 parent
77c7542
commit 51d82a1
Showing
7 changed files
with
48 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
* text=auto | ||
*.js text eol=lf | ||
* text=auto eol=lf |
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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
language: node_js | ||
node_js: | ||
- '10' | ||
- '8' | ||
- '6' | ||
- '4' |
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,9 @@ | ||
/** | ||
* Run promise-returning & async functions in series. | ||
* | ||
* @param tasks - Functions are expected to return a value. If a Promise is returned, it's awaited before continuing with the next task. | ||
* @returns A `Promise` that is fulfilled when all promises returned from calling the functions in `tasks` are fulfilled, or rejects if any of the promises reject. The fulfilled value is an `Array` of the fulfilled values. | ||
*/ | ||
export default function pSeries<ValueType>( | ||
tasks: Iterable<() => Promise<ValueType> | ValueType> | ||
): Promise<ValueType[]>; |
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 |
---|---|---|
@@ -1,21 +1,26 @@ | ||
'use strict'; | ||
|
||
const pReduce = require('p-reduce'); | ||
const is = require('@sindresorhus/is'); | ||
|
||
module.exports = iterable => { | ||
const ret = []; | ||
const pSeries = async tasks => { | ||
const results = []; | ||
|
||
for (const task of iterable) { | ||
for (const task of tasks) { | ||
const type = is(task); | ||
|
||
if (type !== 'Function') { | ||
return Promise.reject(new TypeError(`Expected task to be a \`Function\`, received \`${type}\``)); | ||
throw new TypeError(`Expected task to be a \`Function\`, received \`${type}\``); | ||
} | ||
} | ||
|
||
return pReduce(iterable, (_, fn) => { | ||
return Promise.resolve().then(fn).then(val => { | ||
ret.push(val); | ||
}); | ||
}).then(() => ret); | ||
await pReduce(tasks, async (_, task) => { | ||
const value = await task(); | ||
results.push(value); | ||
}); | ||
|
||
return results; | ||
}; | ||
|
||
module.exports = pSeries; | ||
module.exports.default = pSeries; |
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,9 @@ | ||
import {expectType} from 'tsd-check'; | ||
import pSeries from '.'; | ||
|
||
expectType<Promise<(number | boolean)[]>>( | ||
pSeries<number | boolean>([() => Promise.resolve(1), () => true]) | ||
); | ||
expectType<Promise<(number | boolean)[]>>( | ||
pSeries<number | boolean>(new Set([() => Promise.resolve(1), () => true])) | ||
); |
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 |
---|---|---|
@@ -1,21 +1,20 @@ | ||
import test from 'ava'; | ||
import m from '.'; | ||
import pSeries from '.'; | ||
|
||
test(async t => { | ||
test('pSeries', async t => { | ||
const input = [ | ||
async () => 1 + 1, | ||
() => 2 + 2, | ||
async () => 3 + 3 | ||
]; | ||
|
||
t.deepEqual(await m(input), [2, 4, 6]); | ||
t.deepEqual(await pSeries(input), [2, 4, 6]); | ||
|
||
const fixtureErr = new Error('fixture'); | ||
await t.throws(m([async () => Promise.reject(fixtureErr)]), fixtureErr.message); | ||
const fixtureError = new Error('fixture'); | ||
await t.throwsAsync(pSeries([async () => Promise.reject(fixtureError)]), fixtureError.message); | ||
}); | ||
|
||
test('throw if input is not a function', async t => { | ||
const input = [Promise.resolve(1 + 1)]; | ||
const type = /^v4\./.test(process.version) ? 'Object' : 'Promise'; | ||
await t.throws(m(input), `Expected task to be a \`Function\`, received \`${type}\``); | ||
await t.throwsAsync(pSeries(input), 'Expected task to be a `Function`, received `Promise`'); | ||
}); |