Home > @infiniteobjects/core-library-web > Async > tap
Tap into a promise chain without affecting its value or state. Use this in a .then()
method.
Signature:
static tap<ValueType = unknown>(tapHandler: (value: ValueType) => unknown): (value: ValueType) => Promise<ValueType>;
Parameter | Type | Description |
---|---|---|
tapHandler | (value: ValueType) => unknown | 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. |
Returns:
(value: ValueType) => Promise<ValueType>
A thunk that returns a Promise
.
Promise.resolve('unicorn')
.then(Async.tap(console.log)) // Logs `unicorn`
.then(value => {
// `value` is still `unicorn`
});
getUser()
.then(Async.tap(user => recordStatsAsync(user))) // Stats are saved about `user` async before the chain continues
.then(user => {
// `user` is the user from getUser(), not recordStatsAsync()
});