atexit.js
v1.0.5
Published
Register functions to be called at process termination
Readme
Install
$ npm i atexit.jsFunctions
atexit.register(fn, ...args)Registers a function to the atexit call stack to be called at process termination.
fn: function to be added to atexit call stack....args: arguments to be passed tofn.
atexit.unregister(fn)Unregisters all references of a function from the atexit call stack.
fn: function to be removed from the atexit call stack.
atexit._clear()Clears all functions from the atexit call stack.
atexit._run_exitfuncs()Calls all functions in the atexit call stack then clears it.
atexit._ncallbacks()Returns number of functions in the atexit call stack.
Examples
Register
import * as fs from 'node:fs';
import * as atexit from 'atexit';
const file = '/tmp/example.txt';
atexit.register(fs.appendFileSync, file, '1\n');
atexit.register(fs.appendFileSync, file, '2\n');
atexit.register(fs.appendFileSync, file, '3\n');File Contents:
3
2
1Unregister
import * as fs from 'node:fs';
import * as atexit from 'atexit';
const file = '/tmp/example.txt';
atexit.register(fs.appendFileSync, file, '1\n');
atexit.register(fs.appendFileSync, file, '2\n');
atexit.register(fs.appendFileSync, file, '3\n');
atexit.unregister(fs.appendFileSync);
atexit.register(fs.appendFileSync, file, '4\n');
atexit.register(fs.appendFileSync, file, '5\n');File Contents:
5
4Maintainers
Contributing
PRs accepted.
License
MIT © Brandon Christie
