Need help with p-tap?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

sindresorhus
128 Stars 8 Forks MIT License 15 Commits 0 Opened issues

Description

Tap into a promise chain without affecting its value or state

Services available

!
?

Need anything else?

Contributors list

# 207
node
exec
test-fr...
nextjs
9 commits
# 7,468
TypeScr...
typescr...
iterm2
Node.js
2 commits
# 118,243
HTML
React
Bash
Zsh
1 commit
# 698,664
JavaScr...
TypeScr...
1 commit
# 6,447
chalk
Svelte
example...
Ada
1 commit

p-tap

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(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…

License

MIT © Sindre Sorhus

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.