Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is node.js' console.log asynchronous?

Are console.log/debug/warn/error in node.js asynchrounous? I mean will javascript code execution halt till the stuff is printed on screen or will it print at a later stage?

Also, I am interested in knowing if it is possible for a console.log to NOT display anything if the statement immediately after it crashes node.

like image 779
dhruvbird Avatar asked Feb 26 '11 14:02

dhruvbird


People also ask

Is NodeJS console log async?

"So what?" you probably asked? - This is important part, because console. log() is asynchronous. All asynchronous functions come to Event Table in terms of Event Loop.

Is node JS asynchronous or synchronous?

NodeJS is an asynchronous event-driven JavaScript runtime environment designed to build scalable network applications. Asynchronous here refers to all those functions in JavaScript that are processed in the background without blocking any other request.

Is node JS really asynchronous?

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.

How does NodeJS console log work?

log() function from console class of Node. js is used to display the messages on the console. It prints to stdout with newline. Parameter: This function contains multiple parameters which are to be printed.

What is asynchronous logging in Node JS?

Console logging creates at least one global asynchronous operation (e.g., for standard output), which, according to the async hooks API, generates an outstanding asynchronous operation. Node.js also has other global variables that may cause outstanding asynchronous operations when their module is imported into your application.

Is console log asynchronous or synchronous in Windows?

Console.log is asynchronous in windows while it is synchronous in linux/mac. To make console.log synchronous in windows write this line at the start of your code probably in index.js file.

What is the async hooks API in Node JS?

We can get into a lot of strife working with async operations in JavaScript, but Node.js has a new tool that can help alleviate our pain. It’s called the async hooks API, and we can use it to understand what’s going on with the asynchronous operations in our application.

How do I use console error in Node JS?

console.error ( [data], [...]) The console.error () method works the same as console.log, except that the output is sent to stderr instead of stdout. As stderr is always written to synchronously, therefore in node.js any use of console.error, or other functions that write to stderr, will block your process until the output has all been written.


2 Answers

Update: Starting with Node 0.6 this post is obsolete, since stdout is synchronous now.

Well let's see what console.log actually does.

First of all it's part of the console module:

exports.log = function() {   process.stdout.write(format.apply(this, arguments) + '\n'); }; 

So it simply does some formatting and writes to process.stdout, nothing asynchronous so far.

process.stdout is a getter defined on startup which is lazily initialized, I've added some comments to explain things:

.... code here... process.__defineGetter__('stdout', function() {   if (stdout) return stdout;                            // only initialize it once     /// many requires here ...    if (binding.isatty(fd)) {                             // a terminal? great!     stdout = new tty.WriteStream(fd);   } else if (binding.isStdoutBlocking()) {              // a file?     stdout = new fs.WriteStream(null, {fd: fd});   } else {     stdout = new net.Stream(fd);                        // a stream?                                                          // For example: node foo.js > out.txt     stdout.readable = false;   }    return stdout; }); 

In case of a TTY and UNIX we end up here, this thing inherits from socket. So all that node bascially does is to push the data on to the socket, then the terminal takes care of the rest.

Let's test it!

var data = '111111111111111111111111111111111111111111111111111'; for(var i = 0, l = 12; i < l; i++) {     data += data; // warning! gets very large, very quick }  var start = Date.now(); console.log(data); console.log('wrote %d bytes in %dms', data.length, Date.now() - start); 

Result

....a lot of ones....1111111111111111 wrote 208896 bytes in 17ms  real    0m0.969s user    0m0.068s sys  0m0.012s 

The terminal needs around 1 seconds to print out the sockets content, but node only needs 17 milliseconds to push the data to the terminal.

The same goes for the stream case, and also the file case gets handle asynchronous.

So yes Node.js holds true to its non-blocking promises.

like image 62
Ivo Wetzel Avatar answered Nov 02 '22 14:11

Ivo Wetzel


console.warn() and console.error() are blocking. They do not return until the underlying system calls have succeeded.

Yes, it is possible for a program to exit before everything written to stdout has been flushed. process.exit() will terminate node immediately, even if there are still queued writes to stdout. You should use console.warn to avoid this behavior.

like image 40
Matt Ranney Avatar answered Nov 02 '22 15:11

Matt Ranney