atexit.js
, Python standard library atexit
shipped to Node.js.
-
Let you register cleanup functions.
-
Executed in last-in-first-out order when exit.
Which means, "If you register A, B, and C, at interpreter termination time they will be run in the order C, B, A."
npm i loynoir/atexit.js
// for ESM
import atexit from "atexit"
// for CJS
const atexit = require("atexit");
console.log('open outer door')
// pass arguments
atexit.register(console.log, 'close outer door')
console.log('open inner door')
// pass no argument
atexit.register(()=>console.log('close inner door'))
// Last-in-first-out
/*
open outer door
open inner door
close inner door
close outer door
*/
// when a cleanup function is no more needed, you can unregister it
atexit.unregiter(cleanup_func)
If you want to control in first-in-first-out order, you may use sindresorhus/exit-hook.
In atexit.js
, "it is possible to register the same function and arguments more than once", "all exit handlers have had a chance to run", just like what python standard atexit
does.
# First-in-first-out
$ examples/example_exit-hook.js
borrow a space ship from ๐ฝ
goto mars ๐
one night on mars ๐
exit-hook : return a space ship โฒ
exit-hook : went back from mars ๐
exit-hook : wakeup on mars ๐
# Last-in-first-out
$ examples/example_atexit.py
borrow a space ship from ๐ฝ
goto mars ๐
one night on mars ๐
atexit.py : wakeup on mars ๐
atexit.py : went back from mars ๐
atexit.py : return a space ship โฒ
# Last-in-first-out
$ examples/example_atexit.js
borrow a space ship from ๐ฝ
goto mars ๐
one night on mars ๐
atexit.js : wakeup on mars ๐
atexit.js : went back from mars ๐
atexit.js : return a space ship โฒ
-
sindresorhus/exit-hook - First-in-first-out.
-
loynoir/atexit.js
- Last-in-first-out. -
Python atexit - Python standard library. Last-in-first-out.
-
Golang defer - Golang standard keyword. "Deferred calls are executed in last-in-first-out order."
Modified from sindresorhus/exit-hook