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?
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.
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.
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.
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.
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.
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.
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.
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.
Async:
Sync:
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With