In bluebird / bookshelf.js what the tap function does

What does the bookshelf.js tap function do. I did not find a record in the documentation

return new Library({name: 'Old Books'}) .save(null, {transacting: t}) .tap(function(model) { //code here } 

http://bookshelfjs.org/#Bookshelf-subsection-methods

+7
source share
2 answers

The .tap() uses Bluebird for its promises, and I believe .tap() is one of their specific Promise methods. It looks like it allows you to essentially call .then() without changing the value passed in the chain.

http://bluebirdjs.com/docs/api/tap.html

edit: Due to a request for further clarification, here is an example of the difference between Promise#tap() and Promise#then() . Please note that Promise#tap() not standard and specific to Bluebird.

 var Promise = require('bluebird'); function getUser() { return new Promise(function(resolve, reject) { var user = { _id: 12345, username: 'test', email: ' test@test.com ' }; resolve(user); }); } getUser() .then(function(user) { // do something with `user` console.log('user in then #1:', user); // make sure we return `it`, // so it becomes available to the next promise method return user; }) .tap(function(user) { console.log('user in tap:', user); // note that we are NOT returning `user` here, // because we don't need to with `#tap()` }) .then(function(user) { // and that `user` is still available here, // thanks to using `#tap()` console.log('user in then #2:', user); }) .then(function(user) { // note that `user` here will be `undefined`, // because we didn't return it from the previous `#then()` console.log('user in then #3:', user); }); 
+15
source share

According to Regg "Raganwald" Braithwaite,

tap is a traditional name borrowed from various Unix shell commands. It takes a value and returns a function that always returns a value, but if you pass it a function, it performs a function for side effects. [a source]

This begs the same question as with underscore.js.

The bottom line is this: all clicks are the return of the object that it passed. However, if a function is passed to it, it will perform that function. Thus, it is useful for debugging or for performing side effects in an existing chain without changing this chain.

+1
source share

All Articles