链接承诺和传递价值的简洁方式

时间:2015-03-13 17:36:50

标签: javascript promise bluebird

用Bluebird承诺写这个最简洁的方法是什么:

return someFunc().then(function(result) {
  return otherFunc(result).then(function(foo) {
    ...
  });
});

我看到一些util函数,例如result(),但不完全清楚我将如何/将使用它。基本上我需要调用第二个函数,同时传递第一个函数的结果作为参数。或者这是最简洁的?

2 个答案:

答案 0 :(得分:2)

您可以像这样简化:

return someFunc()
.then(otherFunc)
.then(function(foo) {
    return foo; // assuming you do more here...
});

我希望这不是你的整个代码,或者return的最后一个函数将是无用的,整个将等同于

return someFunc().then(otherFunc);

答案 1 :(得分:1)

.then(function(foo) { return foo; });是多余的。

举个例子,这就是你所需要的。

return someFunc().then(otherFunc);