简体   繁体   English

javascript函数中的异步与同步。 (Node.js的)

[英]Asynchronous vs synchronous in javascript functions. (Node.js)

I am currently going through the nodeschool.io tutorial on Javascript. 我目前正在阅读有关Javascript的nodeschool.io教程。 This is a sample code from one of the solutions. 这是其中一种解决方案的示例代码。

module.exports = function (dir, filterStr, callback) {

  fs.readdir(dir, function (err, list) {
    if (err)
      return callback(err)

    list = list.filter(function (file) {
      return path.extname(file) === '.' + filterStr
    })

    callback(null, list)
  })
}

From what I understand, javascript is a synchronous language. 据我了解,javascript是一种同步语言。 However, node.js uses asynchronous functions. 但是,node.js使用异步函数。 In this code, isn't it possible for the callback to happen before the if statement. 在这段代码中,回调不可能发生在if语句之前。 Therefore, is there even any reason to have functions that are different from the format 因此,甚至有理由拥有与格式不同的功能

    function(params, callback){
      // one line data manipulation
      callback();
    }

node is single threaded, and event based. 节点是单线程的,并且基于事件。 the i/o is what is done asynchronously. I / O是异步完成的。 when you call fs.readdir(), you're passing it a function object as an argument. 当您调用fs.readdir()时,您正在将一个函数对象作为参数传递给它。 think of it as a function pointer in c++. 可以将其视为c ++中的函数指针。 fs.readdir() is going to kick off an asynchronous operation and pass that function object to an event handler that will trigger once the i/o operation has completed. fs.readdir()将启动一个异步操作,并将该函数对象传递给事件处理程序,该事件处理程序将在I / O操作完成后触发。

Now, it doesn't wait for the i/o operation to finish, it just continues to execute code until it completes and then it goes back to listening for events. 现在,它不等待I / O操作完成,它只是继续执行代码,直到完成为止,然后返回侦听事件。

When it receives the event triggered when the i/o operation completes, it will run the code in the function object that it was passed. 当它接收到I / O操作完成时触发的事件时,它将在传递它的函数对象中运行代码。

Because of its single threaded nature, you don't have to worry about those types of race conditions. 由于它具有单线程特性,因此您不必担心这些竞争条件。

From what I understand, javascript is a synchronous language 据我了解,javascript是一种同步语言

I think you are misunderstanding something here. 我认为您在这里误解了一些东西。 Javascript is a single-threaded language. Javascript是一种单线程语言。 But that does not make it synchronous. 但这并不能使其同步。 Nearly every environment in which in runs, including the current biggest ones, browsers and Node.js, provide global event queues for asynchronous processing. 几乎所有正在运行的环境(包括当前最大的环境),浏览器和Node.js,都提供用于异步处理的全局事件队列。

If I understand your question right, I'm also guessing you're confusing levels here. 如果我正确理解您的问题,那么我也在猜测您在此混淆了水平。 The overall module that is exposed here is providing its own asynchronous behavior by accepting a callback that will only be called asynchronously. 此处公开的整个模块通过接受仅被异步调用的回调来提供其自己的异步行为。 But this is built atop another asynchronous block, the call to fs.readdir . 但这是建立在另一个异步块之上,即对fs.readdir的调用。 The callback to that one is the anonymous function that makes up the majority of your code sample. 对该函数的回调是匿名函数,该函数占您代码示例的大部分。 This callback contains the if -statement. 该回调包含 if -statement。 The other one, passed in from the outside, and named callback , will happen in the same order it appears inside that inner function, that is, after the if -statement. 另一个是从外部传入的,名为callback ,其发生顺序与在内部函数中出现的顺序相同,即在if -statement之后。

A proper async function will usually call the callback after a timeout expires or even an event is fired or a message is recieved, meaning that it won't keep the thread busy while waiting, leaving behind a promise. 一个适当的异步函数通常会在超时到期后甚至触发一个事件或接收到一条消息后调用回调,这意味着它不会在等待时使线程保持繁忙,而会留下一个承诺。 In your example the script will continue after you assign the callback, and will call it when the async function's work is done, which will most likely be after the rest of your code is done. 在您的示例中,脚本将在您分配回调之后继续,并在异步函数的工作完成后调用它,这很可能是在其余代码完成之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM