Node.js is single threaded and operates asynchronously using the "Single Threaded Event Loop" architecture and handles multiple concurrent clients efficiently without multithreading. Node.js is actually designed to build distributed applications with many nodes and that's the reason why it's called Node. Actually, the main event loop is single threaded but Node.js uses multiple threads in the background to execute asynchronous code.
Node.js is non-blocking which means that all the callbacks or functions are delegated to the event loop and are executed by different threads. This means that when other processes are executing in a concurrent manner, the event loop continues to run as non-JavaScript operations. This way the tasks are executed asynchronously.
Needless to say that Node.js is extremely good at handling multiple requests concurrently using a single thread model. This is called "asynchronous cooperative multitasking".
Management of asynchronous code in Node.js:
A lot of times we have dealt with multithreading and preemptive multitasking scenarios but the first time we encountered cooperative multitasking using Node.js was when creating a User Module at the earlier stage of our product creation.
This module deals with managing different user roles and permissions and consolidates the respective entitlement of users of different levels (like Admin and User). We understood that asynchronous code in Node.js can be handled in 3 possible ways:
1.Callbacks: Callback is an executable code that is passed as an argument to another code and is expected to execute (or call back) at a specific time. Callbacks make sure that certain code doesn't execute until another code is finished its execution.
2.Using Async/Await with Promises: The async/await pattern structures an asynchronous, non-blocking function similar to a synchronous function. It provides opportunities to the program to execute other code while waiting for a long-running asynchronous task to complete. The await keyword within the async function returns a promise value that ensures the current function is completed before calling the subsequent function.
3.Using Promises: Promises are abstractions provided that give us a way to express ourselves synchronously when writing asynchronous code. A promise returns a value that is either resolved or the reason why that's not resolved (for example, when a network error occurs).
A promise can be in any one of the following states:
States of a Promise
Pending: async operation is incomplete and promise does not have a value yet.
Fullfilled: async operation is completed and promise has a value.
Rejected: async operation has failed and promise will never get fulfilled.
Top comments (0)