在bluebird / bookshelf.js中,tap函数做什么

bookhelf.js tap函数做什么? 我没有在文档中find任何条目

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

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

书架使用Bluebird作为承诺,我相信.tap()是他们特定的Promise方法之一。 看起来像它允许你基本上调用.then()而不改变通过链传递的值。

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

编辑:由于要求进一步澄清,这里是Promise#tap()Promise#then()之间区别的一个例子。 请注意, Promise#tap() 不是标准的,并且是特定于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); }); 

根据Reg“Raganwald”Braithwaite,

tap是从各种Unix shell命令中借用的传统名称。 它接受一个值并返回一个总是返回值的函数,但是如果你传递一个函数,它会执行副作用函数。 [资源]

这里是关于underscore.js提出的同样的问题。

要点是:所有的水龙头都是返回它传递的对象。 但是 ,如果它传递了一个函数,它将执行该函数。 因此,在现有链中debugging或执行副作用而不改变链是有用的。