-
-
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 12.20 and move to ESM
- Loading branch information
1 parent
4e44f94
commit b272464
Showing
7 changed files
with
58 additions
and
85 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
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[]>; |
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,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; | ||
} |
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,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])), | ||
); |
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 |
---|---|---|
@@ -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`', | ||
}); | ||
}); |