Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous operations in javascript [duplicate]

I hear people talking about asynchronous operations in javascript. On the other hand, people say that Javascript is always synchronous sync javascript. Which is true? Is Javascript really asynchronous or synchronous? What do people mean when they talk about asynchronous javascript?

What I have understood is that Javascript on the same page cannot be run concurrently with another block of code of javascript. But for example in ajax requests, while waiting for the server response, one could execute code, and then when the response has arrived, continue with the callback. Though does this mean, that the code that was running while we waited for the server response, will have to finish itself of, or otherwise it will be interrupted?

like image 230
Ville Miekk-oja Avatar asked Jun 19 '16 18:06

Ville Miekk-oja


People also ask

What are asynchronous operations in JavaScript?

Asynchronous programming is a technique that enables your program to start a potentially long-running task and still be able to be responsive to other events while that task runs, rather than having to wait until that task has finished. Once that task has finished, your program is presented with the result.

How does JavaScript do asynchronous processing work?

An async function can contain an await expression, that pauses the execution of the function and waits for the passed Promise's resolution, and then resumes the async function's execution and returns the resolved value. You can think of a Promise in JavaScript as the equivalent of Java's Future or C# 's Task.

What is difference between asynchronous and synchronous in JavaScript?

Asynchronous is a non-blocking architecture, so the execution of one task isn't dependent on another. Tasks can run simultaneously. Synchronous is a blocking architecture, so the execution of each operation is dependent on the completion of the one before it.

Is everything in JavaScript asynchronous?

JavaScript is a single-threaded, non-blocking, asynchronous, concurrent programming language with lots of flexibility. Hold on a second – did it say single-threaded and asynchronous at the same time? If you understand what single-threaded means, you'll likely mostly associate it with synchronous operations.


3 Answers

Javascript can run code asynchronously (i.e., "in the background") but not concurrently (i.e., "at the same time"). The difference is essentially that asynchronous JavaScript code will only run when there's no other code running.

Consider this example:

// print message asynchronously
setTimeout(function() {
  console.log('message');
}, 1000);

// infinite loop
while(true) { } // WARNING: if you actually try to run this, it will
                //          likely crash your browser...

If JavaScript had true concurrency, we would expect that message would be printed after 1000 ms. However, since JavaScript is merely asynchronous, it must wait until no other code is running; since it's stuck in an infinite loop, it will therefore never be able to run the code inside the setTimeout function.

like image 133
Frxstrem Avatar answered Oct 17 '22 07:10

Frxstrem


You may be confusing synchronicity with concurrency. JavaScript does not have concurrency, or running two execution threads at the same time; you have to spawn an entirely new JavaScript interpreter with things like Web Workers or run a cluster of workers in the case of Node to get concurrency.

However, it does have asynchronicity; you register callbacks for events that occur asynchronously, and the event loop calls those callbacks in the sequence they occur. Event callbacks are still non-concurrent, but that doesn't make JavaScript non-asynchronous.

like image 30
Jacob Avatar answered Oct 17 '22 06:10

Jacob


Notice there's a difference between concurrent and asynchronous code. When people say that JavaScript is not "truly asynchronous", they mean that there are no two lines of JavaScript code that will run simultaneously since it is unambiguously single-threaded.

However, it is an asynchronous language in the sense that some functions will execute and process in the background, then trigger a callback function or an event listener when data is ready. All while this is happening, other blocks of JavaScript can run, but again, not while any other lines of JavaScript are running.

like image 35
Patrick Roberts Avatar answered Oct 17 '22 06:10

Patrick Roberts