有条件的Promise.all

时间:2015-11-10 13:14:07

标签: javascript node.js promise

我知道this answer,以下代码基于它。它有效,但我对某些东西不满意。

如果我想根据它进行一些条件检查和解决怎么办?我已经评论了我想要改变的代码。

/**
 * Returns not yet propagate subdomains.
 * @return Promise
 */
Tour.prototype.getOffline = function() {
  var self = this;
  var resolve_, reject_;
  // to be fulfilled later
  var promise = new Promise(function(resolve, reject){
    resolve_ = resolve;
    reject_ = reject;
  });

  var check = function(key) {
    return new Promise(function(resolve, reject) {
      redis_client.hmget(key, 'online', function(err, reply) {
        if (reply[0] === 'n') {
          resolve(key); // <----- I'd like to resolve only here
        } else {
          // reject(); // <---- I can't reject cause of Promise.all
          resolve(); // <----- An empty value to satisfy Promise.all
        }
      });
    });
  };

  this.getKeysRedis('subdomain:*').then(function(resp) {
    var promises = resp.map(check);
    var results = Promise.all(promises);
    results.then(function(data) {

      // I have undefined values on data array

      var array = data.filter(function(key){
        return utils.isDefAndNotNull(key);
      });
      resolve_(array); // <------ How can I resolve without filtering
    });
  }, function(error) {
    reject_(error);
  });
  return promise;
};

最终代码,删除了 Promise构造函数反模式,因为@Bergi警告:

Tour.prototype.getOffline = function() {
  var self = this;

  var check = function(key) {
    return new Promise(function(resolve, reject) {
      redis_client.hmget(key, 'online', function(err, reply) {
        if (reply[0] === 'n') resolve(key);
        else reject();
      });
    });
  };

  var reflect = function(promise) {
    return promise.then(
      x => ({state: 'resolved', value: x}),
      e => ({state: 'rejected' , value: e})
    );
  };

  return new Promise(function(resolve, reject) {
    self.getKeysRedis(self.subdomain_keyname + '*')
    .then(function(keys) {
      return Promise.all(keys.map(check).map(reflect)).then(function(r) {
        return r.filter(x => x.state === 'resolved');
      });
    })
    .then(function(results) {
      var array = results.map((result) => result.value);
      resolve(array);
    })
    .catch((err) => reject(err));
  });
};

1 个答案:

答案 0 :(得分:3)

你基本上想要一个接受承诺的函数,并返回一个在其他承诺结算(履行或拒绝)时履行的承诺:

该功能通常被称为&#34;反映&#34;:

function reflect(promise){
    return promise.then(x => ({state: "fulfilled", value: x}),
                        e => ({state: "rejected" , value: e}));
}

这意味着您可以拒绝(就像您通常那样),然后执行:

this.getKeysRedis('subdomain:*').then(resp => 
  resp.map(check).map(reflect); // note the reflect
).then(Promise.all).then(results => {
    // access results here
    results[0].state; // "fulfilled" if it fulfilled
    results[0].value; // the value or error
});