Antonelli Santos
Antonelli Santos

Reputation: 13

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

(First: I'm sorry, I don't speak english very well!)

I wanna return the results of 3 finds in one array. My code (next) is running well, but I'm in callback hell!

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

I've a FIND inside a FIND inside a FIND. Is there anyway to improve this?

SOLUTION:

_Schema .static('retrieveAll', function(cb) { var model = this;

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

Upvotes: 1

Views: 95

Answers (3)

Yaki Klein
Yaki Klein

Reputation: 4376

take a look at npm Async. It a great library of different patterns that can be used on node.js.

You will probably want to use the waterfall if there is a Chronological priority or parallel pattern if they can all execute in parallel.

Upvotes: 0

Miguel
Miguel

Reputation: 20633

You can try using Promises.

(untested) example:

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

That example is using RSVP, but there are also other promise implementations such as Q and bluebird.

And a side note, you can use concat to concatenate arrays instead of using reduce.

Upvotes: 1

rjatkinson
rjatkinson

Reputation: 498

Some server-side promise libraries like q and bluebird would clean up your code substantially and eliminate the mess of callback hell.

Upvotes: 0

Related Questions