├── .editorconfig ├── .gitattributes ├── .github ├── funding.yml ├── security.md └── 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/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | tidelift: npm/p-waterfall 3 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.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 | - 14 14 | - 12 15 | steps: 16 | - uses: actions/checkout@v2 17 | - uses: actions/setup-node@v2 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable import/export */ 2 | export type Task = ( 3 | previousValue: ValueType 4 | ) => ReturnType | PromiseLike; 5 | 6 | export type InitialTask = () => ReturnType | PromiseLike; 7 | 8 | /** 9 | Run promise-returning & async functions in series, each passing its result to the next. 10 | 11 | @param tasks - Functions are expected to return a value. If a `Promise` is returned, it's awaited before continuing with the next task. 12 | @param initialValue - Value to use as `previousValue` in the first task. 13 | @returns Resolves when all promises returned from calling the functions in `tasks` are fulfilled, or rejects if any of the promises reject. The fulfilled value is the value returned from the last task. 14 | 15 | @example 16 | ``` 17 | import pWaterfall from 'p-waterfall'; 18 | 19 | const tasks = [ 20 | initialValue => getEmoji(initialValue), 21 | previousValue => `I ❤️ ${previousValue}` 22 | ]; 23 | 24 | console.log(await pWaterfall(tasks, 'unicorn')); 25 | //=> 'I ❤️ 🦄' 26 | ``` 27 | */ 28 | export default function pWaterfall( 29 | tasks: readonly [ 30 | InitialTask 31 | ] 32 | ): Promise; 33 | export default function pWaterfall( 34 | tasks: readonly [ 35 | InitialTask, 36 | Task 37 | ] 38 | ): Promise; 39 | export default function pWaterfall( 40 | tasks: readonly [ 41 | InitialTask, 42 | Task, 43 | Task 44 | ] 45 | ): Promise; 46 | export default function pWaterfall( 47 | tasks: readonly [ 48 | InitialTask, 49 | Task, 50 | Task, 51 | Task 52 | ] 53 | ): Promise; 54 | export default function pWaterfall( 55 | tasks: readonly [ 56 | InitialTask, 57 | Task, 58 | Task, 59 | Task, 60 | Task 61 | ] 62 | ): Promise; 63 | export default function pWaterfall( 64 | tasks: readonly [ 65 | InitialTask, 66 | Task, 67 | Task, 68 | Task, 69 | Task, 70 | Task 71 | ] 72 | ): Promise; 73 | export default function pWaterfall< 74 | ValueType1, 75 | ValueType2, 76 | ValueType3, 77 | ValueType4, 78 | ValueType5, 79 | ValueType6, 80 | ReturnType 81 | >( 82 | tasks: readonly [ 83 | InitialTask, 84 | Task, 85 | Task, 86 | Task, 87 | Task, 88 | Task, 89 | Task 90 | ] 91 | ): Promise; 92 | export default function pWaterfall< 93 | ValueType1, 94 | ValueType2, 95 | ValueType3, 96 | ValueType4, 97 | ValueType5, 98 | ValueType6, 99 | ValueType7, 100 | ReturnType 101 | >( 102 | tasks: readonly [ 103 | InitialTask, 104 | Task, 105 | Task, 106 | Task, 107 | Task, 108 | Task, 109 | Task, 110 | Task 111 | ] 112 | ): Promise; 113 | export default function pWaterfall( 114 | tasks: readonly [ 115 | Task 116 | ], 117 | initialValue: ValueType1 118 | ): Promise; 119 | export default function pWaterfall( 120 | tasks: readonly [ 121 | Task, 122 | Task 123 | ], 124 | initialValue: ValueType1 125 | ): Promise; 126 | export default function pWaterfall( 127 | tasks: readonly [ 128 | Task, 129 | Task, 130 | Task 131 | ], 132 | initialValue: ValueType1 133 | ): Promise; 134 | export default function pWaterfall( 135 | tasks: readonly [ 136 | Task, 137 | Task, 138 | Task, 139 | Task 140 | ], 141 | initialValue: ValueType1 142 | ): Promise; 143 | export default function pWaterfall( 144 | tasks: readonly [ 145 | Task, 146 | Task, 147 | Task, 148 | Task, 149 | Task 150 | ], 151 | initialValue: ValueType1 152 | ): Promise; 153 | export default function pWaterfall< 154 | ValueType1, 155 | ValueType2, 156 | ValueType3, 157 | ValueType4, 158 | ValueType5, 159 | ValueType6, 160 | ReturnType 161 | >( 162 | tasks: readonly [ 163 | Task, 164 | Task, 165 | Task, 166 | Task, 167 | Task, 168 | Task 169 | ], 170 | initialValue: ValueType1 171 | ): Promise; 172 | export default function pWaterfall< 173 | ValueType1, 174 | ValueType2, 175 | ValueType3, 176 | ValueType4, 177 | ValueType5, 178 | ValueType6, 179 | ValueType7, 180 | ReturnType 181 | >( 182 | tasks: readonly [ 183 | Task, 184 | Task, 185 | Task, 186 | Task, 187 | Task, 188 | Task, 189 | Task 190 | ], 191 | initialValue: ValueType1 192 | ): Promise; 193 | export default function pWaterfall< 194 | ValueType1, 195 | ValueType2, 196 | ValueType3, 197 | ValueType4, 198 | ValueType5, 199 | ValueType6, 200 | ValueType7, 201 | ValueType8, 202 | ReturnType 203 | >( 204 | tasks: readonly [ 205 | Task, 206 | Task, 207 | Task, 208 | Task, 209 | Task, 210 | Task, 211 | Task, 212 | Task 213 | ], 214 | initialValue: ValueType1 215 | ): Promise; 216 | export default function pWaterfall( 217 | tasks: Iterable>, 218 | initialValue?: unknown 219 | ): Promise; 220 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import pReduce from 'p-reduce'; 2 | 3 | export default async function pWaterfall(iterable, initialValue) { 4 | return pReduce(iterable, (previousValue, function_) => function_(previousValue), initialValue); 5 | } 6 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable symbol-description */ 2 | import {expectType} from 'tsd'; 3 | import pWaterfall from './index.js'; 4 | 5 | expectType>(pWaterfall([() => 'I ❤️ unicorn'])); 6 | expectType>( 7 | pWaterfall([() => 'I ❤️ unicorn', (string: string) => true]) 8 | ); 9 | expectType>( 10 | pWaterfall([ 11 | () => 'I ❤️ unicorn', 12 | (string: string) => true, 13 | (boolean: boolean) => 1 14 | ]) 15 | ); 16 | expectType>( 17 | pWaterfall([ 18 | () => 'I ❤️ unicorn', 19 | (string: string) => true, 20 | (boolean: boolean) => 1, 21 | (number: number) => null 22 | ]) 23 | ); 24 | expectType>( // eslint-disable-line @typescript-eslint/ban-types 25 | pWaterfall([ 26 | () => 'I ❤️ unicorn', 27 | (string: string) => true, 28 | (boolean: boolean) => 1, 29 | (number: number) => Symbol(), 30 | (symbol: symbol) => ({}) 31 | ]) 32 | ); 33 | expectType>( 34 | pWaterfall([ 35 | () => 'I ❤️ unicorn', 36 | (string: string) => true, 37 | (boolean: boolean) => 1, 38 | (number: number) => Symbol(), 39 | (symbol: symbol) => ({}), 40 | (object: Record) => 'foo' 41 | ]) 42 | ); 43 | expectType>( 44 | pWaterfall([ 45 | () => 'I ❤️ unicorn', 46 | (string: string) => true, 47 | (boolean: boolean) => 1, 48 | (number: number) => Symbol(), 49 | (symbol: symbol) => ({}), 50 | (object: Record) => 'foo', 51 | (string: string) => 1 52 | ]) 53 | ); 54 | expectType>( 55 | pWaterfall([ 56 | () => 'I ❤️ unicorn', 57 | (string: string) => true, 58 | (boolean: boolean) => 1, 59 | (number: number) => Symbol(), 60 | (symbol: symbol) => ({}), 61 | (object: Record) => 'foo', 62 | (string: string) => 1, 63 | (number: number) => Symbol() 64 | ]) 65 | ); 66 | 67 | expectType>( 68 | pWaterfall([previousValue => `I ❤️ ${previousValue}`], 'unicorn') 69 | ); 70 | expectType>( 71 | pWaterfall( 72 | [previousValue => `I ❤️ ${previousValue}`, (string: string) => true], 73 | 'unicorn' 74 | ) 75 | ); 76 | expectType>( 77 | pWaterfall( 78 | [ 79 | previousValue => `I ❤️ ${previousValue}`, 80 | (string: string) => true, 81 | (boolean: boolean) => 1 82 | ], 83 | 'unicorn' 84 | ) 85 | ); 86 | expectType>( 87 | pWaterfall( 88 | [ 89 | previousValue => `I ❤️ ${previousValue}`, 90 | (string: string) => true, 91 | (boolean: boolean) => 1, 92 | (number: number) => null 93 | ], 94 | 'unicorn' 95 | ) 96 | ); 97 | expectType>( // eslint-disable-line @typescript-eslint/ban-types 98 | pWaterfall( 99 | [ 100 | previousValue => `I ❤️ ${previousValue}`, 101 | (string: string) => true, 102 | (boolean: boolean) => 1, 103 | (number: number) => Symbol(), 104 | (symbol: symbol) => ({}) 105 | ], 106 | 'unicorn' 107 | ) 108 | ); 109 | expectType>( 110 | pWaterfall( 111 | [ 112 | previousValue => `I ❤️ ${previousValue}`, 113 | (string: string) => true, 114 | (boolean: boolean) => 1, 115 | (number: number) => Symbol(), 116 | (symbol: symbol) => ({}), 117 | (object: Record) => 'foo' 118 | ], 119 | 'unicorn' 120 | ) 121 | ); 122 | expectType>( 123 | pWaterfall( 124 | [ 125 | previousValue => `I ❤️ ${previousValue}`, 126 | (string: string) => true, 127 | (boolean: boolean) => 1, 128 | (number: number) => Symbol(), 129 | (symbol: symbol) => ({}), 130 | (object: Record) => 'foo', 131 | (string: string) => 1 132 | ], 133 | 'unicorn' 134 | ) 135 | ); 136 | expectType>( 137 | pWaterfall( 138 | [ 139 | previousValue => `I ❤️ ${previousValue}`, 140 | (string: string) => true, 141 | (boolean: boolean) => 1, 142 | (number: number) => Symbol(), 143 | (symbol: symbol) => ({}), 144 | (object: Record) => 'foo', 145 | (string: string) => 1, 146 | (number: number) => Symbol() 147 | ], 148 | 'unicorn' 149 | ) 150 | ); 151 | 152 | expectType>( 153 | pWaterfall( 154 | new Set([ 155 | (previousValue: unknown) => { 156 | if (typeof previousValue === 'string') { 157 | return `I ❤️ ${previousValue}`; 158 | } 159 | }, 160 | (string: unknown) => { 161 | if (typeof string === 'string') { 162 | return true; 163 | } 164 | }, 165 | (boolean: unknown) => { 166 | if (typeof boolean === 'boolean') { 167 | return 1; 168 | } 169 | }, 170 | (number: unknown) => { 171 | if (typeof number === 'number') { 172 | return null; 173 | } 174 | }, 175 | (nullValue: unknown) => { 176 | if (nullValue === null) { 177 | return undefined; 178 | } 179 | } 180 | ]), 181 | 'unicorn' 182 | ) 183 | ); 184 | 185 | const tasks1 = [() => 'I ❤️ unicorn'] as const; 186 | expectType>(pWaterfall(tasks1)); 187 | 188 | const tasks2 = [ 189 | () => 'I ❤️ unicorn', 190 | (string: string) => true, 191 | (boolean: boolean) => 1, 192 | (number: number) => null 193 | ] as const; 194 | expectType>(pWaterfall(tasks2)); 195 | -------------------------------------------------------------------------------- /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-waterfall", 3 | "version": "3.0.0", 4 | "description": "Run promise-returning & async functions in series, each passing its result to the next", 5 | "license": "MIT", 6 | "repository": "sindresorhus/p-waterfall", 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" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava && tsd" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "index.d.ts" 24 | ], 25 | "keywords": [ 26 | "promise", 27 | "waterfall", 28 | "series", 29 | "serial", 30 | "sequence", 31 | "sequential", 32 | "ordered", 33 | "task", 34 | "tasks", 35 | "array", 36 | "collection", 37 | "iterable", 38 | "iterator", 39 | "async", 40 | "await", 41 | "promises", 42 | "bluebird" 43 | ], 44 | "dependencies": { 45 | "p-reduce": "^3.0.0" 46 | }, 47 | "devDependencies": { 48 | "ava": "^3.15.0", 49 | "tsd": "^0.14.0", 50 | "xo": "^0.38.2" 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # p-waterfall 2 | 3 | > Run promise-returning & async functions in series, each passing its result to the next 4 | 5 | ## Install 6 | 7 | ``` 8 | $ npm install p-waterfall 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import pWaterfall from 'p-waterfall'; 15 | 16 | const tasks = [ 17 | initialValue => getEmoji(initialValue), 18 | previousValue => `I ❤️ ${previousValue}` 19 | ]; 20 | 21 | console.log(await pWaterfall(tasks, 'unicorn')); 22 | //=> 'I ❤️ 🦄' 23 | ``` 24 | 25 | ## API 26 | 27 | ### pWaterfall(tasks, initialValue?) 28 | 29 | 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 the value returned from the last task. 30 | 31 | #### tasks 32 | 33 | Type: `Iterable` 34 | 35 | Functions are expected to return a value. If a `Promise` is returned, it's awaited before continuing with the next task. 36 | 37 | #### initialValue 38 | 39 | Type: `unknown` 40 | 41 | Value to use as `previousValue` in the first task. 42 | 43 | ## Related 44 | 45 | - [p-series](https://github.com/sindresorhus/p-series) - Run promise-returning & async functions in series 46 | - [p-each-series](https://github.com/sindresorhus/p-each-series) - Iterate over promises serially 47 | - [More…](https://github.com/sindresorhus/promise-fun) 48 | 49 | --- 50 | 51 |
52 | 53 | Get professional support for this package with a Tidelift subscription 54 | 55 |
56 | 57 | Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies. 58 |
59 |
60 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import pWaterfall from './index.js'; 3 | 4 | test('main', async t => { 5 | const input = [ 6 | async previousValue => previousValue + 1, 7 | previousValue => previousValue + 2, 8 | async previousValue => previousValue + 3 9 | ]; 10 | 11 | t.is(await pWaterfall(input, 0), 6); 12 | }); 13 | 14 | test('throws when one of the input functions rejects', async t => { 15 | const fixtureError = new Error('fixture'); 16 | 17 | await t.throwsAsync( 18 | pWaterfall([() => Promise.reject(fixtureError)]), 19 | {message: fixtureError.message} 20 | ); 21 | }); 22 | --------------------------------------------------------------------------------