← Back To API Reference

.tap

.tap(function(any value) handler) -> Promise

Unlike .finally this is not called for rejections.

getUser().tap(function(user) {
    //Like in finally, if you return a promise from the handler
    //the promise is awaited for before passing the original value through
    return recordStatsAsync();
}).then(function(user) {
    //user is the user from getUser(), not recordStatsAsync()
});

Common case includes adding logging to an existing promise chain:

doSomething()
    .then(...)
    .then(...)
    .then(...)
    .then(...)
doSomething()
    .then(...)
    .then(...)
    .tap(console.log)
    .then(...)
    .then(...)

Note: in browsers it is necessary to call .tap with console.log.bind(console) because console methods can not be called as stand-alone functions.