Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous Vs synchronous in NodeJS

I'm new to NodeJS. I have seen there are separate asynchronous and synchronous functions for the same task (ex: {fs.writeFile,fs.writeFileSync} , {fs.read, fs.readSync}).

Can anyone explain why is that? and what is the difference?

like image 234
Udy Warnasuriya Avatar asked Dec 17 '13 13:12

Udy Warnasuriya


People also ask

What is the difference between synchronous and asynchronous functions in JS?

Sync is single-thread, so only one operation or program will run at a time. Async is non-blocking, which means it will send multiple requests to a server. Sync is blocking — it will only send the server one request at a time and will wait for that request to be answered by the server.

What is asynchronous vs synchronous?

The key difference between synchronous and asynchronous communication is synchronous communications are scheduled, real-time interactions by phone, video, or in-person. Asynchronous communication happens on your own time and doesn't need scheduling.

Why node js is asynchronous programming?

Node. js uses callbacks, being an asynchronous platform, it does not wait around like database query, file I/O to complete. The callback function is called at the completion of a given task; this prevents any blocking, and allows other code to be run in the meantime.

Is node js always asynchronous?

js is an asynchronous event-driven JavaScript runtime and is the most effective when building scalable network applications. Node. js is free of locks, so there's no chance to dead-lock any process.

What is asynchronous and synchronous in JavaScript?

Async and sync are probably two of the most heard words among javascript developers, they refer to asynchronous and synchronous programming respectively. Asynchronous programming in javascript can be done using callbacks, Promise, and async and await. Javascript handles asynchronous tasks with the help of event loop.

Why do we need both sync and async in Node JS?

The reason for having both sync and async verisons of those operations is that they can be time-consuming. Since node.js has a single-threaded main event loop, you do not under any circumstances want to block the event loop with slow synchronous function calls. That is why everything is done using callbacks/promises/futures instead.

What is asynchronous request in Ajax?

Asynchronous (AJAX Web-Application Model) An asynchronous request doesn’t block the client i.e. browser is responsive. At that time, user can perform another operations also. In such case, javascript engine of the browser is not blocked.

Is asynchronous JavaScript delaying your user interface?

This may not look like a big problem but when you see it in a bigger picture you realize that it may lead to delaying the User Interface. Let us see the example how Asynchronous JavaScript runs.


2 Answers

Async:

  1. Send request
  2. go on with other code
  3. response come in any time on a callback

Sync:

  1. Send request
  2. Wait for response
  3. go on with other code after response
like image 95
Cracker0dks Avatar answered Sep 22 '22 13:09

Cracker0dks


The reason for having both sync and async verisons of those operations is that they can be time-consuming. Since node.js has a single-threaded main event loop, you do not under any circumstances want to block the event loop with slow synchronous function calls.

That is why everything is done using callbacks/promises/futures instead. This way, you can have an event loop that just calls an async function and handle the result of the async function in a callback, when it happens to be done.

This is one of the major strengths of node.js, and one of the basic rules: "do not block the main event loop".

like image 39
Frost Avatar answered Sep 24 '22 13:09

Frost