p-tap
Tap into a promise chain without affecting its value or state
Last updated 6 years ago by sindresorhus .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install p-tap 
SYNC missed versions from official npm registry.

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

Current Tags

  • 4.0.0                                ...           latest (4 years ago)

5 Versions

  • 4.0.0                                ...           4 years ago
  • 3.1.0                                ...           6 years ago
  • 3.0.0                                ...           6 years ago
  • 2.0.0                                ...           6 years ago
  • 1.0.0                                ...           8 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (4)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |