Not phased work. Is it normal?

For example, after a complex cycle that processes the data from the database and writes them to a certain array, I need to process the data from the array. What happens is that the function of array processing is triggered without waiting for the mining cycle (filling in the array). Everything has to be done through the setTimeout (); Is this normal?

checkDialog: function (data, callback) {
    var interlocutorsArray = JSON.parse(data);
    var dialogsId = {};
    mongoclient.open(function (err, cursor) {

        var db = cursor.db('messages');
        var dialogsDb = db.collection('dialog');
        interlocutorsArray.forEach(function (item, index) {
            dialogsDb.find({
                owner: item.owner_user_id,
                viewer: item.viewer_user_id
            }, function (err, cursor) {
                cursor.count(function (err, count) {
                    if (count === 0) {
                        dialogsDb.insert({
                            owner: item.owner_user_id,
                            viewer: item.viewer_user_id
                        }, function (err, inserting) {
                            dialogsId[inserting[0]._id] = {
                                owner: item.owner_user_id,
                                viewer: item.viewer_user_id
                            };
                        });
                    } else {
                        cursor.each(function (err, item) {
                            if (item !== null) {
                                dialogsId[item._id] = {
                                    owner: item.owner,
                                    viewer: item.viewer
                                };
                            }
                        });
                    }
                });
            });
        });
    });

    setTimeout((function () {
        callback(dialogsId);
    }), 150);

    console.log('after callback');

}

Sorry for my bad English!

First, I recommend you to switch from your current mongodb module to something more comfortable, such as:

I'll be using monk in my example, by it may be easily adapted for any mongodb module, including native mongodb node driver.

As for your problem, you may solve it with any module for asynchronous control flow management, such as:

Here's an example of solving your problem with async.js:

checkDialog: function (data, callback) {
  var db = require('monk')('localhost/messages');
  var dialogsDb = db.get('dialog');
  var interlocutorsArray = JSON.parse(data);

  async.concat(interlocutorsArray, function (item, next) {
    var query = {
      owner: item.owner_user_id
      viewer: item.viewer_user_id
    };
    dialogsDb.find(query, function (err, docs) {
      if (err) return next(err);
      if (docs && docs.length > 0) {
        next(null, docs);
      } else {
        dialogsDb.insert(query, function (err, inserting) {
          next(err, [inserting]);
        })
      }
    })
  }, function (err, items) {
    if (err) throw err; // it's best to handle errors without throwing them
    var dialogsId = {};
    for (var item, i = 0; i < items.length; i++) {
      item = items[i];
      dialogsId[item._id] = {
        owner: item.owner,
        viewer: item.viewer
      };
    }
    callback(dialogsId);
  })
}