🚪 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 …


This content originally appeared on DEV Community and was authored by Randall

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());

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);
}

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.


This content originally appeared on DEV Community and was authored by Randall


Print Share Comment Cite Upload Translate Updates
APA

Randall | Sciencx (2021-10-13T01:29:43+00:00) 🚪 Really exit!. Retrieved from https://www.scien.cx/2021/10/13/%f0%9f%9a%aa-really-exit/

MLA
" » 🚪 Really exit!." Randall | Sciencx - Wednesday October 13, 2021, https://www.scien.cx/2021/10/13/%f0%9f%9a%aa-really-exit/
HARVARD
Randall | Sciencx Wednesday October 13, 2021 » 🚪 Really exit!., viewed ,<https://www.scien.cx/2021/10/13/%f0%9f%9a%aa-really-exit/>
VANCOUVER
Randall | Sciencx - » 🚪 Really exit!. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/13/%f0%9f%9a%aa-really-exit/
CHICAGO
" » 🚪 Really exit!." Randall | Sciencx - Accessed . https://www.scien.cx/2021/10/13/%f0%9f%9a%aa-really-exit/
IEEE
" » 🚪 Really exit!." Randall | Sciencx [Online]. Available: https://www.scien.cx/2021/10/13/%f0%9f%9a%aa-really-exit/. [Accessed: ]
rf:citation
» 🚪 Really exit! | Randall | Sciencx | https://www.scien.cx/2021/10/13/%f0%9f%9a%aa-really-exit/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.