Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 12, 2021
1 parent 4e44f94 commit b272464
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 85 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 8
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
49 changes: 17 additions & 32 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,21 @@
declare const pSeries: {
/**
Run promise-returning & async functions in series.
/**
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.
@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.
@example
```
import pSeries = require('p-series');
import got = require('got');
@example
```
import pSeries from 'p-series';
import got from 'got';
(async () => {
const tasks = [
() => got('https://sindresorhus.com'),
() => checkSomething(),
() => doSomethingElse()
];
const tasks = [
() => got('https://sindresorhus.com'),
() => checkSomething(),
() => doSomethingElse()
];
console.log(await pSeries(tasks));
})();
```
*/
<ValueType>(tasks: Iterable<() => Promise<ValueType> | ValueType>): Promise<
ValueType[]
>;

// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function pSeries<ValueType>(
// tasks: Iterable<() => Promise<ValueType> | ValueType>
// ): Promise<ValueType[]>;
// export = pSeries;
default: typeof pSeries;
};

export = pSeries;
console.log(await pSeries(tasks));
```
*/
export default function pSeries<ValueType>(tasks: Iterable<() => Promise<ValueType> | ValueType>): Promise<ValueType[]>;
27 changes: 8 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
'use strict';
const pReduce = require('p-reduce');
const is = require('@sindresorhus/is');
export default async function pSeries(tasks) {
for (const task of tasks) {
if (typeof task !== 'function') {
throw new TypeError(`Expected task to be a \`Function\`, received \`${typeof task}\``);
}
}

const pSeries = async tasks => {
const results = [];

for (const task of tasks) {
const type = is(task);

if (type !== 'Function') {
throw new TypeError(`Expected task to be a \`Function\`, received \`${type}\``);
}
results.push(await task()); // eslint-disable-line no-await-in-loop
}

await pReduce(tasks, async (_, task) => {
const value = await task();
results.push(value);
});

return results;
};

module.exports = pSeries;
// TODO: Remove this for the next major release
module.exports.default = pSeries;
}
10 changes: 5 additions & 5 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {expectType} from 'tsd';
import pSeries = require('.');
import pSeries from './index.js';

expectType<Promise<(number | boolean)[]>>(
pSeries<number | boolean>([() => Promise.resolve(1), () => true])
expectType<Promise<Array<number | boolean>>>(
pSeries<number | boolean>([async () => Promise.resolve(1), () => true]),
);
expectType<Promise<(number | boolean)[]>>(
pSeries<number | boolean>(new Set([() => Promise.resolve(1), () => true]))
expectType<Promise<Array<number | boolean>>>(
pSeries<number | boolean>(new Set([async () => Promise.resolve(1), () => true])),
);
14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -38,13 +40,9 @@
"promises",
"bluebird"
],
"dependencies": {
"@sindresorhus/is": "^0.15.0",
"p-reduce": "^2.1.0"
},
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"ava": "^3.15.0",
"tsd": "^0.17.0",
"xo": "^0.44.0"
}
}
24 changes: 12 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

> Run promise-returning & async functions in series
*Note:* You can just use `await` in a for-loop to get the same behavior. This package was useful before async/await existed.

If you're doing the same work in each function, use [`p-each-series`](https://github.com/sindresorhus/p-each-series) instead.

See [`p-all`](https://github.com/sindresorhus/p-all) for a concurrent counterpart.
Expand All @@ -15,18 +17,16 @@ $ npm install p-series
## Usage

```js
const pSeries = require('p-series');
const got = require('got');

(async () => {
const tasks = [
() => got('https://sindresorhus.com'),
() => checkSomething(),
() => doSomethingElse()
];

console.log(await pSeries(tasks));
})();
import pSeries from 'p-series';
import got from 'got';

const tasks = [
() => got('https://sindresorhus.com'),
() => checkSomething(),
() => doSomethingElse()
];

console.log(await pSeries(tasks));
```

## API
Expand Down
12 changes: 8 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import test from 'ava';
import pSeries from '.';
import pSeries from './index.js';

test('main', async t => {
const input = [
async () => 1 + 1,
() => 2 + 2,
async () => 3 + 3
async () => 3 + 3,
];

t.deepEqual(await pSeries(input), [2, 4, 6]);

const fixtureError = new Error('fixture');
await t.throwsAsync(pSeries([async () => Promise.reject(fixtureError)]), fixtureError.message);
await t.throwsAsync(pSeries([async () => Promise.reject(fixtureError)]), {
message: fixtureError.message,
});
});

test('throw if input is not a function', async t => {
const input = [Promise.resolve(1 + 1)];
await t.throwsAsync(pSeries(input), 'Expected task to be a `Function`, received `Promise`');
await t.throwsAsync(pSeries(input), {
message: 'Expected task to be a `Function`, received `object`',
});
});

0 comments on commit b272464

Please sign in to comment.