Git Product home page Git Product logo

atexit.js's Introduction

atexit.js

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."

Installation

npm i loynoir/atexit.js

Usage

// 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)

Differece

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 โฒ

Related & Comparison

  • 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."

Disclaimer

Modified from sindresorhus/exit-hook

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.