同步与异步 Nodejs

Synchronous vs Asynchronous Nodejs

我在使用 Mocha 测试框架自动化网页时,遇到了同步代码和异步代码这两个术语。 当您发送 HTTP 请求时,我熟悉同步和异步事件……但我从未听说过代码是同步和异步的。 任何人都愿意解释......我在之前的问题上看到它与回调有关,但即便如此我仍然对这个概念很困惑。

同步代码将严格逐行处理,而异步代码将继续处理下一行,而前几行代码仍在处理。

对于异步代码,在以下代码段中,您会希望 WorldHello 之前记录到控制台,因为数据库查询需要更多计算机资源,因此需要更多时间。 console.log('World') 将在查询之前完成执行。

var Person = mongoose.model('Person', personSchema);

Person.findOne({ 'name.last': 'Ghost' }, function(err, person) {
  if(err) 
    throw err;
  else
    console.log('Hello');
});

console.log('World');

在同步代码中,Hello 将在 World 之前记录,因为查询在执行任何后续行之前完成执行。

下面是我的服务器代码的简化版本。我演示了 synchronous 代码(在你开始执行一个操作之后,在它完成之前不会开始进一步的操作)和 asynchronous 代码(你开始执行一个操作,然后继续做其他操作,稍后你"callback"或者得到第一个操作的结果。)

这有一些重要的后果。几乎每次调用异步函数时:

  • 您的异步函数中的 return 值是无用的,因为该函数将立即 return,尽管找到结果需要很久了。

  • 需要等回调函数执行后才能获取结果

  • 调用异步函数后的代码行将在异步回调函数运行之前执行。

例如,我下面代码中 console.logs 的顺序是:

line 3 - before sync
line 8 - after sync, before async
line 16 - after async call, outside callback
line 14 - inside async call and callback



// synchronous operations execute immediately, subsequent code
// doesn't execute until the synchronous operation completes.
console.log('line 3 - before sync');
var app = require('express')();
var cfgFile = require('fs').readFileSync('./config.json');
var cfg = JSON.parse(cfgFile);
var server = require('http').createServer(app);
console.log('line 8 - after sync, before async');

// When you call an asynchronous function, something starts happening,
// and the callback function will be run later:
server.listen(cfg.port, function(){
  // Do things that need the http server to be started
  console.log('line 14 - inside async call and callback');
});
console.log('line 16 - after async call, outside callback');

同步代码 基本上意味着代码行按照它们编写的顺序执行。同步事件将立即执行关联的回调,就像直接调用函数一样,因此最终结果是相同的。

一般来说,异步代码与调用代码分开并发执行。例如调用 setTimeout 将立即 return 并执行下一个表达式,但它会启动一个动作,在将来的某个时间点它会触发调用指定回调的 异步事件

Mocha 测试框架支持这两种类型的代码,它为异步代码提供 done() 回调。 done() 是让 Mocha 知道测试何时完成的回调。

异步测试代码,来自http://mochajs.org/

describe('User', function() {
  describe('#save()', function() {
    it('should save without error', function(done) { // done is a callback
      var user = new User('Luna');
      user.save(function(err) { // save is async db call that starts here, completes whenever
        if (err) throw err;
        done();
      });
    });
  });
});

测试同步代码不需要done(),希望测试代码按顺序执行。所以当执行完最后一个表达式时,测试就结束了。