├── .npmrc ├── .gitattributes ├── .gitignore ├── .editorconfig ├── index.js ├── .github └── workflows │ └── main.yml ├── test.js ├── package.json ├── license └── readme.md /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | class EndBreakError extends Error { 2 | constructor(value) { 3 | super(); 4 | this.value = value; 5 | } 6 | } 7 | 8 | const pBreak = async value => { 9 | throw new EndBreakError(value); 10 | }; 11 | 12 | pBreak.end = async error => { 13 | if (error instanceof EndBreakError) { 14 | return error.value; 15 | } 16 | 17 | throw error; 18 | }; 19 | 20 | export default pBreak; 21 | -------------------------------------------------------------------------------- /.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@v1 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import pBreak from './index.js'; 3 | 4 | const fixture = Symbol('fixture'); 5 | 6 | test('break', async t => { 7 | const result = await Promise.resolve(fixture) 8 | .then(value => pBreak(value)) 9 | .then(() => 'noop') 10 | .catch(pBreak.end); 11 | 12 | t.is(result, fixture); 13 | }); 14 | 15 | test('non-break error', async t => { 16 | t.plan(1); 17 | 18 | const errorFixture = new Error('fixture'); 19 | 20 | await Promise.resolve(fixture) 21 | .then(() => Promise.reject(errorFixture)) 22 | .catch(pBreak.end) 23 | .catch(error => { 24 | t.is(error, errorFixture); 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "p-break", 3 | "version": "2.0.0", 4 | "description": "Break out of a promise chain", 5 | "license": "MIT", 6 | "repository": "sindresorhus/p-break", 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" 20 | }, 21 | "files": [ 22 | "index.js" 23 | ], 24 | "keywords": [ 25 | "promise", 26 | "break", 27 | "chain", 28 | "out", 29 | "return", 30 | "early", 31 | "if", 32 | "conditional", 33 | "error", 34 | "thunk", 35 | "function", 36 | "async", 37 | "await", 38 | "promises", 39 | "combinator", 40 | "bluebird" 41 | ], 42 | "devDependencies": { 43 | "ava": "^3.15.0", 44 | "xo": "^0.38.2" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # p-break 2 | 3 | > Break out of a promise chain 4 | 5 | See ["How do I break out of a promise chain?"](https://github.com/sindresorhus/promise-fun#how-do-i-break-out-of-a-promise-chain) for a better way. 6 | 7 | ## Install 8 | 9 | ``` 10 | $ npm install p-break 11 | ``` 12 | 13 | ## Usage 14 | 15 | Here the `onlyRunConditional` promises are skipped if `conditional` is falsy: 16 | 17 | ```js 18 | import pBreak from 'p-break'; 19 | 20 | alwaysRun1() 21 | .then(() => alwaysRun2()) 22 | .then(conditional => conditional || pBreak('🦄')) 23 | .then(() => onlyRunConditional1()) 24 | .then(() => onlyRunConditional2()) 25 | .then(() => onlyRunConditional3()) 26 | .then(() => onlyRunConditional4()) 27 | .catch(pBreak.end) 28 | .then(console.log); 29 | //=> '🦄' 30 | ``` 31 | 32 | ## API 33 | 34 | ### pBreak([value]) 35 | 36 | Starts the break. Any `.then()`'s between here and `pBreak.end()` are skipped. 37 | 38 | ### value 39 | 40 | Value to pass down the chain after `pBreak.end()`. 41 | 42 | ### pBreak.end 43 | 44 | Ends the break. Make sure not to have any other `.catch()` handlers between `pBreak()` and here. 45 | 46 | ## Related 47 | 48 | - [p-if](https://github.com/sindresorhus/p-if) - Conditional promise chains 49 | - [More…](https://github.com/sindresorhus/promise-fun) 50 | --------------------------------------------------------------------------------