stack

Home > @infiniteobjects/core-library-web > Async > tap

Async.tap() method

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>;

Parameters

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.

Example 1


Promise.resolve('unicorn')
  .then(Async.tap(console.log)) // Logs `unicorn`
  .then(value => {
    // `value` is still `unicorn`
  });

Example 2


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()
  });