我正在阅读有关Bookshelf的本教程.Bookshelf使用Bluebird承诺.有很多例子看起来像这样:
var getEvents = function(participantId) {
return new models.Participant()
.query({where: {id: participantId}})
.fetch({withRelated: ['events'], require: true})
.then(function(model) {
return model;
});
};
Run Code Online (Sandbox Code Playgroud)
我对承诺仍然不满意,但从我到目前为止所学到的东西看起来很奇怪.我的问题是,上述功能是否与fetch()直接返回并离开最终版本完全相同then():
var getEvents = function(participantId) {
return new models.Participant()
.query({where: {id: participantId}})
.fetch({withRelated: ['events'], require: true});
};
Run Code Online (Sandbox Code Playgroud)
也就是说,它仍然做同样的事情,返回相同的承诺,可以以相同的方式调用,等等?
根据我的理解,传递给函数的参数then获取链中前一个promise的返回值.所以,在我看来.then(function (a) { return a; }),一般来说只是一个无操作.对?
如果它们不一样,有什么区别?发生了什么,为什么作者这样写呢?