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

2019-04-16 10:24发布

问题:

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