【问题标题】:Get states of tasks ran by bluebird.all().spread()获取 bluebird.all().spread() 运行的任务状态
【发布时间】:2014-05-04 17:41:07
【问题描述】:

我有两个由 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() 部分中,我能够检查每个任务的状态。
This is the code I was using before Bluebird

有了蓝鸟,我得到了:

remotes
ftpRemotes

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

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

【问题讨论】:

  • 如果你想让事情失败,你可以使用Promise.settle,但我怀疑这就是你真正需要的。
  • 这两个函数只为每个返回一个数组,我认为这无关紧要。顺便说一下源代码在这个 PR github.com/zaggino/brackets-git/pull/288
  • “我有回应”是什么意思?那是一些日志吗?在哪里?
  • q 如果特定任务顺利,则在“value”键中返回“fullfilled”字符串,如果出现问题则返回“rejected”。
  • @FezVrasta:你没有使用Q.all,而是Q.allSettled,是吗?

标签: javascript promise bluebird


【解决方案1】:

如果你想处理案件,他们是好是坏:

//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.
  });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多