Skip to content

Commit

Permalink
Require Node.js 8, add TypeScript definition, update dependencies (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Mar 12, 2019
1 parent 77c7542 commit 51d82a1
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 26 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
9 changes: 9 additions & 0 deletions index.d.ts
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[]>;
23 changes: 14 additions & 9 deletions index.js
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;
9 changes: 9 additions & 0 deletions index.test-d.ts
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]))
);
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd-check"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"promise",
Expand All @@ -37,11 +38,12 @@
"bluebird"
],
"dependencies": {
"@sindresorhus/is": "^0.7.0",
"@sindresorhus/is": "^0.15.0",
"p-reduce": "^1.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^1.3.1",
"tsd-check": "^0.3.0",
"xo": "^0.24.0"
}
}
13 changes: 6 additions & 7 deletions test.js
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`');
});

0 comments on commit 51d82a1

Please sign in to comment.