简体   繁体   中英

Understanding What is Sync and What is Async in Node.js/JavaScript

Suppose I have the following code in a Node.js app:

function bar(){
    //do_stuff
    return value;
}

app.get('/',function(req,res){
    var result = bar();
    res.send(result);
});
  1. Suppose that in function bar //do_stuff is a while loop up to 10 million. Is it guaranteed that the function will complete, return its value which will be assigned to result , and only then res.send(result) will be executed?

  2. What if in function bar //do_stuff involved database queries (and the return value depended on those queries). In this case could I be sure that res.send(result) would be executed with the correct value, returned upon completion of function bar ?

Async is basically everything, which interfere with environment outside your JavaScript. And not because it's necessary, but because it is pragmatic . Async is good for time consuming tasks, because it do not blocks the main thread of JS.

In case of while loop, there is usually no need to make it async, although you can make it async, if you will (eg you know it will take a long time to compute and you do not want to block your main thread). If the HTTP request or database query would not be async, the JS engine will just stop and do nothing until the result arrive, which is not a big problem in Node.js, but in case of web pages, this will freeze the UI, which will certainly anger some users.

There are several ways to solve async tasks

  • callbacks -- function, that handles async task, accepts as parameter another function, ie callback , that will be called as soon as the task is finished (usually it accepts callback for success and callback for error case).
  • events -- function, that handles async task, immediately returns object , which is a event emitter . You attach callbacks to events, which this emitter will generate and these callbacks are then called by the emitter as the events occurs. This callbacks are noted as event listeners .
  • promise -- function, that handles async task, immediately returns object , which represents a promise of some future result. This promise has methods like then() , fail() , always() , through which you can register your callbacks. It's quite similar to event emitter, promises offers better data flow control, but when created, cannot be aborted.
  • ES next -- future versions of JavaScript brings ES6 generators or ES7 async functions.

You can distinguish async by the fact, that it do not returns the result immediately, but it usually allows you to register callbacks for later invocation, either via parameters/arguments, or via methods of returned object.

Note: in case of ES6 and ES7, there are no callbacks needed for async process, but I will not complicate this topic more with this new technology.

for loop does not allow you to register callbacks, it simply occupies JS engine and makes all the iteration as fast as it can.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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