How does a node js process know when to stop

0 votes

Since many node.js scripts follow a pattern of doing something asynchronously (example below), how do they know when to stop?

In the following code, how does node determine after processing the writeFile, and registering the callback appropriately, that the process should be kept alive until the callback(s) run?

fs = require('fs');

fs.writeFile('foo', 'cat', function() {
  console.log('wrote to foo!'); 
  fs.readFile('foo', 'utf8', function(err, data) {
    console.log(data);
  });
}); 
Nov 30, 2020 in Node-js by kartik
• 37,510 points
545 views

1 answer to this question.

0 votes

Hello,

node keeps track of all outstanding work requests. Your fs.writefile() call creates a work request for I/O and adds your callback to that request. node saves the work request into its tables at the same time it is starting the I/O activity. Your code execution exits when you reach the end of your function. (But your memory/variables/etc. remain)

Later the I/O finishes and node takes the work request out of its tables. It sees the callback attached to the request and so calls that function with the results of the I/O request. Your global data is still around and any variables in closures still exist, so it seems to your code like it never stopped.

If you do nothing more, don't make any more requests, then when you return from your functions node will stop, because then there won't be any remaining requests in the queues.

answered Nov 30, 2020 by Niroj
• 82,880 points

Related Questions In Node-js

0 votes
1 answer

How to print a stack trace in Node.js?

Hello @kartik, To print stacktrace of Error in console in ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,880 points
627 views
0 votes
1 answer

How to stop node.js program from command line?

Hello @kartik, Ctrl+Z suspends it, which means it can ...READ MORE

answered Jul 15, 2020 in Node-js by Niroj
• 82,880 points
18,515 views
0 votes
1 answer

How to host a Node.Js application in shared hosting?

Hello @kartik, You can run node.js server on a typical ...READ MORE

answered Jul 17, 2020 in Node-js by Niroj
• 82,880 points
6,726 views
0 votes
1 answer

How to create a directory if it doesn't exist using Node.js?

Hello @kartik, Try this: var fs = require('fs'); var dir ...READ MORE

answered Jul 20, 2020 in Node-js by Niroj
• 82,880 points
555 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
21,910 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,690 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,561 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
41,894 views
0 votes
1 answer

How to create a directory if it doesn't exist using Node.js?

Hello @kartik, Try: var fs = require('fs'); var dir = ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,880 points
5,734 views
0 votes
1 answer

How to process POST data in Node.js?

Hello @kartik, You can use the querystring module: var qs = ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,880 points
2,344 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP