An event loop in Node.js handles all the asynchronous callbacks in an application . It is one of the most important aspects of Node.js and the reason behind Node.js have non-blocking I/O. Since Node.js is an event-driven language, you can easily attach a listener to an event and then when the event occurs the callback will be executed by the specific listener. Whenever functions like setTimeout, http.get, and fs.readFile are called, Node.js executed the event loop and then proceeds with the further code without waiting for the output. Once the entire operation is finished, Node.js receives the output and then executes the callback function. This is why all the callback functions are placed in a queue in a loop. Once the response is received, they are executed one by one.
A promise is an object that may produce a single value some time in the future : either a resolved value, or a reason that it’s not resolved (e.g., a network error occurred). A promise is an object which can be returned synchronously from an asynchronous function. It will be in one of 3 possible states: Fulfilled: onFulfilled() will be called (e.g., resolve() was called) Rejected: onRejected() will be called (e.g., reject() was called) Pending: not yet fulfilled or rejected A promise is settled if it’s not pending (it has been resolved or rejected). Sometimes people use resolved and settled to mean the same thing: not pending . Once settled, a promise can not be resettled. Calling resolve() or reject() again will have no effect. The immutability of a settled promise is an important feature. Promises following the spec must follow a specific set of rules: A promise or...
fork() spawn() fork() is a particular case of spawn() that generates a new instance of a V8 engine. Spawn() launches a new process with the available set of commands. Multiple workers run on a single node code base for multiple tasks. This method doesn’t generate a new V8 instance and only a single copy of the node module is active on the processor.
Comments
Post a Comment