1

我有两个由 Bluebird 运行的任务:

// Require bluebird...
var Promise = require("bluebird");

// Run two tasks together
Promise
  .all([Git.getRemotes(), GitFtp.getFtpRemotes()])
  .spread(function (remotes, ftpRemotes) {
    // Something cool
  });

使用q.js我得到了回应:

remotes.value (the response of my task)
remotes.state ("fullfilled" or "rejected" depending if the task thrown an error or not)
ftpRemotes.value
ftpRemotes.state

因此,在该spread()部分中,我能够检查每个任务的状态。
这是我在 Bluebird 之前使用的代码

有了蓝鸟,我得到了:

remotes
ftpRemotes

仅包含我的任务生成的数组。

我想我需要Promise.allSettled,但在文档中找不到。
如何获取每个任务的状态?

4

1 回答 1

7

如果你想处理这个案子,它们是好是坏:

//Require bluebird...
var Promise = require("bluebird");

// Run two tasks together
Promise
  .all([Git.getRemotes(), GitFtp.getFtpRemotes()])
  .spread(function (remotes, ftpRemotes) {
    // Something cool
  }).catch(function(err){
    // handle errors on both
  });

如果您想等待两者都解决或拒绝使用Promise.settle

Promise
  .settle([Git.getRemotes(), GitFtp.getFtpRemotes()])
  .spread(function(remotesStatus,ftpRemoteStatus){
        // the two are PromiseInspection objects and have:
        // isFullfilled, isRejected, value() etc.
  });
于 2014-03-26T11:28:27.837 回答