Find inside callback of another find(...),如何跳出回调地狱?

Find inside callback of another find (...), how to escape from callback hell?

(首先:对不起,我的英语说得不是很好!)

我想 return 在一个数组中找到 3 个结果。 我的代码(下一个)很好 运行,但我在回调地狱!

_Schema
  .static('retrieveAll', function(cb) {
    query = {};
    this.find(query, function(err, data) {
      if(err) {
        cb(err, null);
        return;
      }

      if(data)
        all = data;
      else
        all = [];

      _StoresModel.find(query).select('contact address').exec(function(err, data) {
        if(err) {
          cb(err, null);
          return;
        }

        if(data) {
          all = data.reduce(function(coll, item) {
            coll.push(item);
            return coll;
          }, all);
        }

        _CustomersModel.find(query).select('contact address').exec(function(err, data) {
          if(err) {
            cb(err, null);
            return;
          }

          if(data) {
            all = data.reduce(function(coll, item) {
              coll.push(item);
              return coll;
            }, all);
          }

          cb(null, all);          
        });
      });
    });
  });

我在 FIND 中的 FIND 中找到了 FIND。 有什么办法可以改善吗?

解决方案:

_架构 .static('retrieveAll', 函数(cb) { var 模型 = 这个;

_async.parallel(
  { contacts: function(cb) {
      model.find({}).exec(cb);
    }
  , stores: function(cb) {
      _StoresModel.find({}).select('contact address').exec(cb);
    }
  , costumers: function(cb) {
      _CostumersModel.find({}).select('contact address').exec(cb);
    }
  }
, function(err, data) {
  if(err) {
    cb(err, null);
    return
  }

  var ret = [];
  if(data.contacts.length > 0) {
    ret = ret.concat(data.contacts);
  }
  if(data.stores.length > 0) {
    ret = ret.concat(data.stores);
  }
  if(data.costumers.length > 0) {
    ret = ret.concat(data.costumers);
  }

  cb(null, ret);
});

一些服务器端承诺库,如 q and bluebird 将大大清理您的代码并消除回调地狱的混乱。

您可以尝试使用 Promises

(未经测试)示例:

var RSVP = require('rsvp');
var all = [];

_Schema.static('retrieveAll', function(cb) {
    query = {};

    findPromise(this, query)
    .then(function (data) {
        all = data;
        return findPromise(_StoresModel, query, 'contact address');
    })
    .then(function (stores) {
        all = all.concat(stores);
        return findPromise(_CustomersModel, query, 'contact address');
    })
    .then(function (customers) {
        all = all.concat(customers);
        cb(null, all);
    })
    .catch(function (err) {
        cb(err, null);
    });
});

function findPromise(Model, query, select) {
    return new RSVP.Promise(function (resolve, reject) {
        Model.find(query).select(select || '*').exec(function (err, data) {
            return err ? reject(err) : resolve(data);
        });
    });
}

该示例使用 RSVP, but there are also other promise implementations such as Q and bluebird

附带说明,您可以使用 concat 来连接数组,而不是使用 reduce

看看 npm Async。它是一个很棒的不同模式库,可以在 node.js.

上使用

如果它们都可以并行执行,您可能希望使用 waterfall if there is a Chronological priority or parallel 模式。