Get states of tasks ran by bluebird.all().spread()

老子叫甜甜 提交于 2019-12-06 14:26:17

问题


I have two tasks ran by Bluebird:

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

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

With q.js I had as response:

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

So inside the spread() part I was able to check the state of each task.
This is the code I was using before Bluebird

With bluebird I get just:

remotes
ftpRemotes

Containing just the array generated by my tasks.

I think I need Promise.allSettled but I can't find it in the documentation.
How can I get the state of each task?


回答1:


If you want to handle the case they're good/bad together:

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

If you want to wait for both to either resolve or reject use Promise.settle:

Promise
  .settle([Git.getRemotes(), GitFtp.getFtpRemotes()])
  .spread(function(remotesStatus,ftpRemoteStatus){
        // the two are PromiseInspection objects and have:
        // isFullfilled, isRejected, value() etc.
  });


来源:https://stackoverflow.com/questions/22658992/get-states-of-tasks-ran-by-bluebird-all-spread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!