DEV Community

Randall
Randall

Posted on • Updated on

πŸšͺ Really exit!

Here's a funny (and really unimportant) thing I discovered about Node.js today.

First, for anyone who may not know, if you ever want to see the source code of a JavaScript function, you can just call .toString() on the function. (if it's a JavaScript function, and not one that's implemented in native code inside of the JS engine itself)

Let's try it on process.exit:

console.log(process.exit.toString());
Enter fullscreen mode Exit fullscreen mode

Here's what we get as output:

function exit(code) {
  if (code || code === 0)
    process.exitCode = code;

  if (!process._exiting) {
    process._exiting = true;
    process.emit('exit', process.exitCode || 0);
  }
  // FIXME(joyeecheung): This is an undocumented API that gets monkey-patched
  // in the user land. Either document it, or deprecate it in favor of a
  // better public alternative.
  process.reallyExit(process.exitCode || 0);
}
Enter fullscreen mode Exit fullscreen mode

It turns out that process.exit() is just a thin wrapper around another function, process.reallyExit()! process.reallyExit() is a native function, so we can't inspect its code in this manner (but you can find it in Node's source code on GitHub if you're interested).

This tickled me. It's a good reminder that there are always some funny, hacky things in the technologies we know and love.

Top comments (5)

Collapse
 
lioness100 profile image
Lioness100

LOL omg this is hilarious

Collapse
 
nguyenit67 profile image
Nguyen

Woww

Collapse
 
chiefgui profile image
Guilherme Oderdenge

lol

Collapse
 
foxy4096 profile image
Aditya

Sneaky picky
😏

Collapse
 
ankush981 profile image
Ankush Thakur

Lovely!