有没有一种更干净的方式来把这些蓝鸟的承诺联系起来

Is there a cleaner way to chain these Bluebird promises?

本文关键字:蓝鸟 起来 联系 承诺 方式 有没有 一种      更新时间:2023-09-26

我有三个函数(A, B, C),每个函数都返回一个promise。承诺链不需要任何来自前一个承诺的信息,除了它们已经完成。

B必须等待A完成,C必须等待B完成。

目前我有:

return A(thing)
.then(function () {
  return B(anotherThing);
})
.then(function () {
  return C(somethingElse);
});

感觉我浪费了很多空间(7行代码实际上只有3行代码)。

works

return A(thing)
    .then(B.bind(null,anotherThing))
    .then(C.bind(null,somethingElse));

注意:bind在IE8或更早版本上不可用-但有一个polyfill - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

在ES2015中,你可以这样做- iojs将允许你启用箭头功能,但它们显然在某些方面被破坏了

return A(thing)
    .then(() => B(anotherThing))
    .then(() => C(somethingElse));