DEV Community

Randall
Randall

Posted on β€’ Edited on

11 1

πŸšͺ 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.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

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
Adzy β€’

Sneaky picky
😏

Collapse
 
ankush981 profile image
Ankush Thakur β€’

Lovely!

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay