64

GitHub - sindresorhus/p-tap: Tap into a promise chain without affecting its valu...

 5 years ago
source link: https://github.com/sindresorhus/p-tap
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

readme.md

p-tap Build Status

Tap into a promise chain without affecting its value or state

Install

$ npm install p-tap

Usage

const pTap = require('p-tap');

Promise.resolve('unicorn')
	.then(pTap(console.log)) // Logs `unicorn`
	.then(value => {
		// `value` is still `unicorn`
	});
const pTap = require('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()
	});
const pTap = require('p-tap');

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

API

pTap(input)

Use this in a .then() method.

Returns a thunk that returns a Promise.

pTap.catch(input)

Use this in a .catch() method.

Returns a thunk that returns a Promise.

input

Type: Function

Any return value is ignored. Exceptions thrown in input are relayed back to the original promise chain.

If input 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…

License

MIT © Sindre Sorhus


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK