├── .npmrc ├── .gitattributes ├── .gitignore ├── .editorconfig ├── index.js ├── .github └── workflows │ └── main.yml ├── index.test-d.ts ├── package.json ├── license ├── test.js ├── readme.md └── index.d.ts /.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 | const pTap = tapHandler => async value => { 2 | await tapHandler(value); 3 | return value; 4 | }; 5 | 6 | pTap.catch = tapHandler => async error => { 7 | await tapHandler(error); 8 | throw error; 9 | }; 10 | 11 | export default pTap; 12 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable @typescript-eslint/no-floating-promises */ 2 | import {expectType} from 'tsd'; 3 | import pTap from './index.js'; 4 | 5 | Promise.resolve('unicorn') 6 | .then( 7 | pTap(value => { 8 | expectType(value); 9 | return 1; 10 | }) 11 | ) 12 | .then(value => { 13 | expectType(value); 14 | }); 15 | 16 | Promise.reject(new Error('fixture')).catch( 17 | pTap.catch(error => { 18 | expectType(error); 19 | }) 20 | ); 21 | 22 | Promise.resolve('unicorn') 23 | .then( 24 | pTap(value => { 25 | expectType(value); 26 | return 1; 27 | }) 28 | ); 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "p-tap", 3 | "version": "4.0.0", 4 | "description": "Tap into a promise chain without affecting its value or state", 5 | "license": "MIT", 6 | "repository": "sindresorhus/p-tap", 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 | "tap", 28 | "debug", 29 | "log", 30 | "then", 31 | "catch", 32 | "error", 33 | "chain", 34 | "pipeline", 35 | "thunk", 36 | "function", 37 | "async", 38 | "await", 39 | "promises", 40 | "bluebird" 41 | ], 42 | "devDependencies": { 43 | "ava": "^3.15.0", 44 | "delay": "^5.0.0", 45 | "time-span": "^4.0.0", 46 | "tsd": "^0.14.0", 47 | "xo": "^0.38.2" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import delay from 'delay'; 3 | import timeSpan from 'time-span'; 4 | import pTap from './index.js'; 5 | 6 | const fixture = Symbol('unicorn'); 7 | const fixtureError = new Error('unicorn'); 8 | const tapError = new Error('tap error!'); 9 | 10 | test('ignores tap value', async t => { 11 | const value = await Promise.resolve(fixture) 12 | .then(pTap(tapValue => { 13 | t.is(tapValue, fixture); 14 | return 'ignored-val'; 15 | })); 16 | 17 | t.is(value, fixture); 18 | }); 19 | 20 | test('does not ignore tap error', async t => { 21 | await Promise.resolve(fixture) 22 | .then(pTap(() => { 23 | throw tapError; 24 | })) 25 | .catch(error => { 26 | t.is(error, tapError); 27 | }); 28 | }); 29 | 30 | test('waits for tap promise to resolve', async t => { 31 | const end = timeSpan(); 32 | const value = await Promise.resolve(fixture).then(pTap(() => delay(200))); 33 | 34 | t.is(value, fixture); 35 | t.true(end() > 180); 36 | }); 37 | 38 | test('catch - ignores tap value', async t => { 39 | t.plan(2); 40 | 41 | await Promise.reject(fixtureError) 42 | .catch(pTap.catch(error => { 43 | t.is(error, fixtureError); 44 | return 'ignored-val'; 45 | })) 46 | .catch(error => { 47 | t.is(error, fixtureError); 48 | }); 49 | }); 50 | 51 | test('catch - does not ignore tap error', async t => { 52 | t.plan(1); 53 | 54 | await Promise.reject(fixtureError) 55 | .catch(pTap.catch(() => { 56 | throw tapError; 57 | })) 58 | .catch(error => { 59 | t.is(error, tapError); 60 | }); 61 | }); 62 | 63 | test('catch - waits for tap promise to resolve', async t => { 64 | const end = timeSpan(); 65 | 66 | await Promise.reject(fixtureError) 67 | .catch(pTap.catch(() => delay(200))) 68 | .catch(error => { 69 | t.is(error, fixtureError); 70 | t.true(end() > 180); 71 | }); 72 | }); 73 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # p-tap 2 | 3 | > Tap into a promise chain without affecting its value or state 4 | 5 | ## Install 6 | 7 | ``` 8 | $ npm install p-tap 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import pTap from 'p-tap'; 15 | 16 | Promise.resolve('unicorn') 17 | .then(pTap(console.log)) // Logs `unicorn` 18 | .then(value => { 19 | // `value` is still `unicorn` 20 | }); 21 | ``` 22 | 23 | ```js 24 | import pTap from 'p-tap'; 25 | 26 | getUser() 27 | .then(pTap(user => recordStatsAsync(user))) // Stats are saved about `user` async before the chain continues 28 | .then(user => { 29 | // `user` is the user from getUser(), not recordStatsAsync() 30 | }); 31 | ``` 32 | 33 | ```js 34 | import pTap from 'p-tap'; 35 | 36 | Promise.resolve(() => doSomething()) 37 | .catch(pTap.catch(console.error)) // Prints any errors 38 | .then(handleSuccess) 39 | .catch(handleError); 40 | ``` 41 | 42 | ## API 43 | 44 | ### pTap(tapHandler) 45 | 46 | Use this in a `.then()` method. 47 | 48 | Returns a [thunk](https://en.wikipedia.org/wiki/Thunk) that returns a `Promise`. 49 | 50 | ### pTap.catch(tapHandler) 51 | 52 | Use this in a `.catch()` method. 53 | 54 | Returns a [thunk](https://en.wikipedia.org/wiki/Thunk) that returns a `Promise`. 55 | 56 | #### tapHandler 57 | 58 | Type: `Function` 59 | 60 | Any return value is ignored. Exceptions thrown in `tapHandler` are relayed back to the original promise chain. 61 | 62 | If `tapHandler` returns a `Promise`, it will be awaited before passing through the original value. 63 | 64 | ## Related 65 | 66 | - [p-log](https://github.com/sindresorhus/p-log) - Log the value/error of a promise 67 | - [p-if](https://github.com/sindresorhus/p-if) - Conditional promise chains 68 | - [p-catch-if](https://github.com/sindresorhus/p-catch-if) - Conditional promise catch handler 69 | - [More…](https://github.com/sindresorhus/promise-fun) 70 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | declare const pTap: { 2 | /** 3 | Tap into a promise chain without affecting its value or state. Use this in a `.then()` method. 4 | 5 | @param tapHandler - Any return value is ignored. Exceptions thrown in `tapHandler` are relayed back to the original promise chain. If `tapHandler` returns a `Promise`, it will be awaited before passing through the original value. 6 | @returns A [thunk](https://en.wikipedia.org/wiki/Thunk) that returns a `Promise`. 7 | 8 | @example 9 | ``` 10 | import pTap from 'p-tap'; 11 | 12 | Promise.resolve('unicorn') 13 | .then(pTap(console.log)) // Logs `unicorn` 14 | .then(value => { 15 | // `value` is still `unicorn` 16 | }); 17 | 18 | getUser() 19 | .then(pTap(user => recordStatsAsync(user))) // Stats are saved about `user` async before the chain continues 20 | .then(user => { 21 | // `user` is the user from getUser(), not recordStatsAsync() 22 | }); 23 | ``` 24 | */ 25 | (tapHandler: (value: ValueType) => unknown): ( 26 | value: ValueType 27 | ) => Promise; 28 | 29 | /** 30 | Tap into a promise chain without affecting its value or state. Use this in a `.catch()` method. 31 | 32 | @param tapHandler - Any return value is ignored. Exceptions thrown in `tapHandler` are relayed back to the original promise chain. If `tapHandler` returns a `Promise`, it will be awaited before passing through the original value. 33 | @returns A [thunk](https://en.wikipedia.org/wiki/Thunk) that returns a `Promise`. 34 | 35 | @example 36 | ``` 37 | import pTap from 'p-tap'; 38 | 39 | Promise.resolve(() => doSomething()) 40 | .catch(pTap.catch(console.error)) // Prints any errors 41 | .then(handleSuccess) 42 | .catch(handleError); 43 | ``` 44 | */ 45 | catch( 46 | tapHandler: (error: ErrorType) => unknown 47 | ): (error: ErrorType) => Promise; 48 | }; 49 | 50 | export default pTap; 51 | --------------------------------------------------------------------------------