Tap into a promise chain without affecting its value or state
Tap into a promise chain without affecting its value or state
$ npm install p-tap
const pTap = require('p-tap');Promise.resolve('unicorn') .then(pTap(console.log)) // Logs
unicorn
.then(value => { //value
is stillunicorn
});
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);
Use this in a
.then()method.
Returns a thunk that returns a
Promise.
Use this in a
.catch()method.
Returns a thunk that returns a
Promise.
Type:
Function
Any return value is ignored. Exceptions thrown in
tapHandlerare relayed back to the original promise chain.
If
tapHandlerreturns a
Promise, it will be awaited before passing through the original value.
MIT © Sindre Sorhus