是“.then(function(a){return a;})”一个没有承诺的承诺?

我正在阅读关于Bookshelf的这个教程 。 书架使用蓝鸟承诺。 有不less例子看起来像这样:

var getEvents = function(participantId) { return new models.Participant() .query({where: {id: participantId}}) .fetch({withRelated: ['events'], require: true}) .then(function(model) { return model; }); }; 

我对承诺仍然不满意,但从目前我所了解到的情况来看,这似乎很奇怪。 我的问题是,上面的函数和直接返回fetch()完全一样,离开最后的then()

 var getEvents = function(participantId) { return new models.Participant() .query({where: {id: participantId}}) .fetch({withRelated: ['events'], require: true}); }; 

也就是说,它仍然做同样的事情,返回相同的承诺,可以用同样的方式调用,等等?

据我所知,传递给函数的参数then得到链中前一个promise的返回值。 所以,在我看来.then(function (a) { return a; })通常只是一个空操作。 对?

如果他们不一样,有什么区别? 发生了什么事,作者为什么这样写呢?

在我看来,似乎.then(function (a) { return a; })只是一个没有操作。 对?

是。 1

这是没用的,应该省略。

发生了什么事,作者为什么这样写呢?

这是一个错误。 或者作者不了解承诺。

1:如果不一样,有什么区别?

一如既往,有一些边缘情况。 真奇怪的。 没有人应该使用(没有广泛的评论):
a)它返回一个新的promise实例,一个独特的对象,以避免共享。 不过, .then()也是。
b)再次testinga的可靠性。 如果自履行以来突然成为承诺,现在将等待。 这当然是可怕的。

Bergi的答案是对的,但仅仅是为了展示一个不是没有任何操作的例子,这是一个非人为的例子:

 o = {}; Promise.resolve(o).then(o => o.then = () => {}); // make thenable Promise.resolve(o).then(console.log); // logs the object Promise.resolve(o).then(x => x).then(console.log); // doesn't log the object 

一般来说,不要这样做then(function(a) { return a; })