├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 16 14 | steps: 15 | - uses: actions/checkout@v2 16 | - uses: actions/setup-node@v2 17 | with: 18 | node-version: ${{ matrix.node-version }} 19 | - run: npm install 20 | - run: npm test 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Run promise-returning & async functions in series. 3 | 4 | @param tasks - Functions are expected to return a value. If a Promise is returned, it's awaited before continuing with the next task. 5 | @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. 6 | 7 | @example 8 | ``` 9 | import pSeries from 'p-series'; 10 | import got from 'got'; 11 | 12 | const tasks = [ 13 | () => got('https://sindresorhus.com'), 14 | () => checkSomething(), 15 | () => doSomethingElse() 16 | ]; 17 | 18 | console.log(await pSeries(tasks)); 19 | ``` 20 | */ 21 | export default function pSeries(tasks: Iterable<() => Promise | ValueType>): Promise; 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | export default async function pSeries(tasks) { 2 | for (const task of tasks) { 3 | if (typeof task !== 'function') { 4 | throw new TypeError(`Expected task to be a \`Function\`, received \`${typeof task}\``); 5 | } 6 | } 7 | 8 | const results = []; 9 | 10 | for (const task of tasks) { 11 | results.push(await task()); // eslint-disable-line no-await-in-loop 12 | } 13 | 14 | return results; 15 | } 16 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import pSeries from './index.js'; 3 | 4 | expectType>>( 5 | pSeries([async () => Promise.resolve(1), () => true]), 6 | ); 7 | expectType>>( 8 | pSeries(new Set([async () => Promise.resolve(1), () => true])), 9 | ); 10 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "p-series", 3 | "version": "3.0.0", 4 | "description": "Run promise-returning & async functions in series", 5 | "license": "MIT", 6 | "repository": "sindresorhus/p-series", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": "^12.20.0 || ^14.13.1 || >=16.0.0" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava && tsd" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "index.d.ts" 24 | ], 25 | "keywords": [ 26 | "promise", 27 | "series", 28 | "serial", 29 | "sequence", 30 | "sequential", 31 | "ordered", 32 | "task", 33 | "tasks", 34 | "array", 35 | "collection", 36 | "iterable", 37 | "iterator", 38 | "async", 39 | "await", 40 | "promises", 41 | "bluebird" 42 | ], 43 | "devDependencies": { 44 | "ava": "^3.15.0", 45 | "tsd": "^0.17.0", 46 | "xo": "^0.44.0" 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # p-series 2 | 3 | > Run promise-returning & async functions in series 4 | 5 | *Note:* You can just use `await` in a for-loop to get the same behavior. This package was useful before async/await existed. 6 | 7 | If you're doing the same work in each function, use [`p-each-series`](https://github.com/sindresorhus/p-each-series) instead. 8 | 9 | See [`p-all`](https://github.com/sindresorhus/p-all) for a concurrent counterpart. 10 | 11 | ## Install 12 | 13 | ``` 14 | $ npm install p-series 15 | ``` 16 | 17 | ## Usage 18 | 19 | ```js 20 | import pSeries from 'p-series'; 21 | import got from 'got'; 22 | 23 | const tasks = [ 24 | () => got('https://sindresorhus.com'), 25 | () => checkSomething(), 26 | () => doSomethingElse() 27 | ]; 28 | 29 | console.log(await pSeries(tasks)); 30 | ``` 31 | 32 | ## API 33 | 34 | ### pSeries(tasks) 35 | 36 | 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. 37 | 38 | #### tasks 39 | 40 | Type: `Iterable` 41 | 42 | Functions are expected to return a value. If a Promise is returned, it's awaited before continuing with the next task. 43 | 44 | ## Related 45 | 46 | - [p-all](https://github.com/sindresorhus/p-all) - Run promise-returning & async functions concurrently with optional limited concurrency 47 | - [p-waterfall](https://github.com/sindresorhus/p-waterfall) - Run promise-returning & async functions in series, each passing its result to the next 48 | - [p-each-series](https://github.com/sindresorhus/p-each-series) - Iterate over promises serially 49 | - [More…](https://github.com/sindresorhus/promise-fun) 50 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import pSeries from './index.js'; 3 | 4 | test('main', async t => { 5 | const input = [ 6 | async () => 1 + 1, 7 | () => 2 + 2, 8 | async () => 3 + 3, 9 | ]; 10 | 11 | t.deepEqual(await pSeries(input), [2, 4, 6]); 12 | 13 | const fixtureError = new Error('fixture'); 14 | await t.throwsAsync(pSeries([async () => Promise.reject(fixtureError)]), { 15 | message: fixtureError.message, 16 | }); 17 | }); 18 | 19 | test('throw if input is not a function', async t => { 20 | const input = [Promise.resolve(1 + 1)]; 21 | await t.throwsAsync(pSeries(input), { 22 | message: 'Expected task to be a `Function`, received `object`', 23 | }); 24 | }); 25 | --------------------------------------------------------------------------------