如何强制调用在node.js中是同步的

时间:2012-05-22 04:10:14

标签: javascript node.js mongodb express

请查看下面的代码段。我希望第二个MongoDB查询只有在得到第一个查询的结果后才能执行。但正如你可以想象的那样,它不会按顺序发生。

db.collection('student_profile', function (err, stuColl) {
  //This if-else can be refactored in to a seperate function
  if (req.params.stuId == req.header('x-stuId')) {
    var stuInQuestion = student;
  } else {
    var stuInQuestionId = new ObjectID.createFromHexString(req.params.stuId);
    stuColl.findOne({
      '_id': stuInQuestionId
    }, function (err, stuInQuestionObj) {
      if (!stuInQuestionObj) {
        process.nextTick(function () {
          callback(null);
        });
      } else {
        var stuInQuestion = stuInQuestionObj;
      }
    });
  }

  stuColl.find({
    '_id': {
      $in: stuInQuestion['courses']
    }
  }).limit(25).sort({
    '_id': -1
  }).toArray(function (error, courses) {
    if (error) {
      process.nextTick(function () {
        callback(null);
      });
    } else {
      process.nextTick(function () {
        callback(courses);
      });
    }
  });
});

那么我的选择是什么?

  1. 有没有办法以不需要任何控制流库的方式对其进行编码?如果是,有人可以告诉我怎么做?

  2. 如果确实需要使用控制流程库,我应该使用哪一个? asynch,FuturesJs,还有什么?

2 个答案:

答案 0 :(得分:4)

您不需要任何控制流库,因为这些库只是在后台使用回调。

尝试这样的事情:

db.collection('student_profile', function (err, collection) {

  // Suppose that `firstQuery` and `secondQuery` are functions that make your
  // queries and that they're already defined.
  firstQuery(function firstCallback(err, firstRes) {

    // Do some logic here.

    // Make your second query inside the callback
    secondQuery(function secondCallback(err, secondRes) {
      // More logic here
    });
  });
});

基本上,您要做的是从第一个查询的回调中调用第二个查询。

这可能会让你深深陷入困境。如果这成为一个问题,您可以通过定义函数而不是内联所有函数,并将逻辑包装在模块内来缓解它。

答案 1 :(得分:0)

我会创建一个findStudent函数并让两种情况都称之为:

db.collection('student_profile', function (err, stuColl) {
  //This if-else can be refactored in to a seperate function
  if (req.params.stuId == req.header('x-stuId')) {
    return findStudent(student);
  } else {
    var stuInQuestionId = new ObjectID.createFromHexString(req.params.stuId);
    stuColl.findOne({
      '_id': stuInQuestionId
    }, function (err, stuInQuestionObj) {
      if (!stuInQuestionObj) {
        process.nextTick(function () {
          callback(null);
        });
      } else {
        return findStudent(stuInQuestionObj);
      }
    });
  }

  function findStudent(stuInQuestion) {
    stuColl.find({
      '_id': {
        $in: stuInQuestion['courses']
      }
    }).limit(25).sort({
      '_id': -1
    }).toArray(function (error, courses) {
      if (error) {
        process.nextTick(function () {
          callback(null);
        });
      } else {
        process.nextTick(function () {
          callback(courses);
        });
      }
    });
  }
});
相关问题