gpt4 book ai didi

node.js - 在 bluebird/bookshelf.js 中,tap 函数是做什么的

转载 作者:搜寻专家 更新时间:2023-10-31 22:17:21 25 4
gpt4 key购买 nike

bookshelf.js 的tap 函数是做什么的。我没有在文档中找到任何条目

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

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

最佳答案

Bookshelf 使用 Bluebird 作为他们的 promise ,我相信 .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);
});

关于node.js - 在 bluebird/bookshelf.js 中,tap 函数是做什么的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36442773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com