0

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?

6
  • So your first assumption is correct. If you make a function and call that function all of your code will complete before res.send. Now if in those functions you make any database queries it will not wait for those queries to complete before moving on. Commented Oct 10, 2016 at 19:50
  • What else apart from database queries the runtime will not wait for? Where can I find more information about that behavior? Commented Oct 10, 2016 at 19:51
  • Anything that involves leaving your current node enviornment. So for instance if you have several microservices and you call one service from another, node will not wait for the callback before it does a res.send. So essentially if your code is leaving your current node space then it will not wait. However there are ways to force it to wait. Commented Oct 10, 2016 at 19:53
  • You can do database queries both synchronously or asynchronously. Without knowing the API you are using, the second question is not answerable. You have to read the docs to know whether you'll get the result back from the call (in a blocking fashion) or asynchronously (via promises/callbacks). Commented Oct 10, 2016 at 19:54
  • If function bar does db query it's best done in a non blocking fashion... aka async; then you are expected to wait for it's return before invoking app.get(...){...}. Hence callback or promises. Commented Oct 10, 2016 at 19:55

1 Answer 1

2

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 (e.g. 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, i.e. 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.

Sign up to request clarification or add additional context in comments.

6 Comments

"you know it will take a long time to compute" --- unless asynchronous tasks should be IO-bound. It is probably bad worded, but "computation" sounds like something that is CPU-bound.
@zerkms, in browser land, we have a WebWorker, which allows to run some computial intensive code in separate thread or we can create a promise and resolve it in setTimeout(callback, 0), which will create something like second thread and will not block main thread. Maybe it is not asynchronous in nature, but it is treated the same way.
webworkers are part of web api, not the ES standard. Not sure mentioning them is relevant when the question is about the (a)synchronous nature of the language.
"future versions of JavaScript brings ES6 generators" actually ES6 is the current version of JS! (Watch this be invalid in a year or two.)
@towerofnix actually the current is ES2016, the ES6 (ES2015) is one year old at the moment :-)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.