p-tap
TypeScript icon, indicating that this package has built-in type declarations

4.0.0 • Public • Published

p-tap

Tap into a promise chain without affecting its value or state

Install

$ npm install p-tap

Usage

import pTap from 'p-tap';

Promise.resolve('unicorn')
	.then(pTap(console.log)) // Logs `unicorn`
	.then(value => {
		// `value` is still `unicorn`
	});
import pTap from 'p-tap';

getUser()
	.then(pTap(user => recordStatsAsync(user))) // Stats are saved about `user` async before the chain continues
	.then(user => {
		// `user` is the user from getUser(), not recordStatsAsync()
	});
import pTap from 'p-tap';

Promise.resolve(() => doSomething())
	.catch(pTap.catch(console.error)) // Prints any errors
	.then(handleSuccess)
	.catch(handleError);

API

pTap(tapHandler)

Use this in a .then() method.

Returns a thunk that returns a Promise.

pTap.catch(tapHandler)

Use this in a .catch() method.

Returns a thunk that returns a Promise.

tapHandler

Type: Function

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.

Related

  • p-log - Log the value/error of a promise
  • p-if - Conditional promise chains
  • p-catch-if - Conditional promise catch handler
  • More…

Package Sidebar

Install

npm i p-tap

Weekly Downloads

6,127

Version

4.0.0

License

MIT

Unpacked Size

5.41 kB

Total Files

5

Last publish

Collaborators

  • sindresorhus