Home > Enterprise >  How asynchronous code runs in node.js without using browser
How asynchronous code runs in node.js without using browser

Time:02-14

asynchronous code such as setTimeout can only be run by using a browser; as the engine hands over the code to the browser till the time of execution comes. But in node, we have only an engine and no browser then how does setTimeout work on the node.

CodePudding user response:

Asynchronous code is achieved using Promises, and optionally the async/await syntactic sugar.

Promises are implemented using raw JavaScript, and has nothing to do with whether the code is ran in the browser or not.

In your case you are referring to setTimeout. This function simply delays code execution by scheduling for the function to be called in JavaScript's event loop.

This is part of the core engine of JavaScript, and once again has nothing to do with the browser.

What makes you think that the browser makes any difference here?

Any implementation will have a mechanism in place for being able to wait for a certain amount of time to pass, this is not something special to a browser.

  • Related