在 bluebird / bookshelf.js tap 函数中有什么作用
in bluebird / bookshelf.js what does tap function do
bookshelf.js tap 函数有什么作用。我没有在文档中找到任何条目
return new Library({name: 'Old Books'})
.save(null, {transacting: t})
.tap(function(model) {
//code here
}
Bookshelf 使用 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 `user` from `#then()`,
// 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 is a traditional name borrowed from various Unix shell commands.
It takes a value and returns a function that always returns the value,
but if you pass it a function, it executes the function for
side-effects. [source]
Here 与 underscore.js 提出的问题相同。
要点是这样的:tap 所做的只是 return 它传递的对象。 但是,如果它被传递一个函数,它将执行那个函数。因此,它对于调试或在现有链中执行副作用而不改变该链非常有用。
bookshelf.js tap 函数有什么作用。我没有在文档中找到任何条目
return new Library({name: 'Old Books'})
.save(null, {transacting: t})
.tap(function(model) {
//code here
}
Bookshelf 使用 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 `user` from `#then()`,
// 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 is a traditional name borrowed from various Unix shell commands. It takes a value and returns a function that always returns the value, but if you pass it a function, it executes the function for side-effects. [source]
Here 与 underscore.js 提出的问题相同。
要点是这样的:tap 所做的只是 return 它传递的对象。 但是,如果它被传递一个函数,它将执行那个函数。因此,它对于调试或在现有链中执行副作用而不改变该链非常有用。