SkillAgentSearch skills...

Unload

Run a piece of code when the javascript process stops. Works in all environments (browsers, nodejs..)

Install / Use

/learn @pubkey/Unload
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

javascript: unload

Run a piece of code whenever the javascript process stops/exits/quits. On browsers, nodejs, electron, react-native, workers, iframes. It also ensures that the exit-function is called only once.

You should use this module when your write a npm library where you dont know in which environments the users will run it.

What does unload handle?

When nodejs:

process.on('beforeExit');
process.on('exit');
process.on('SIGINT'); // catches ctrl+c event
process.on('uncaughtException'); // catches uncaught exceptions

When browser:

window.addEventListener('beforeunload'); // closing of normal browser-window
window.addEventListener('unload'); // closed inside of iframe

Usage

Installation:

npm install unload --save

Add a function which runs when the process exits:

var unload = require('unload');
unload.add(function(){
    console.log('Ouch, I\'m dying.');
});

Add and remove the function (It will no longer run when the process exits):

var unload = require('unload');
var ret = unload.add(function(){
    console.log('Ouch, I\'m dying.');
});

ret.remove(); // removes the event-handler

Run all previously added functions:

var unload = require('unload');
unload.add(function(){
    console.log('Ouch, I\'m dying.');
});

unload.runAll();

Remove all added functions (They will no longer run when the process exits):

var unload = require('unload');
unload.add(function(){
    console.log('Ouch, I\'m dying.');
});

unload.removeAll();

Sponsored by

<p align="center"> <a href="https://rxdb.info/?utm_source=github&utm_medium=repo&utm_campaign=github-unload"> <img src="https://rxdb.info/files/logo/logo_text.svg" alt="Sponsored by RxDB" width="300" /> <br /> <br /> <span>The <b>JavaScript Database</b></span> </a> </p>

Related Skills

View on GitHub
GitHub Stars57
CategoryDevelopment
Updated7d ago
Forks7

Languages

JavaScript

Security Score

100/100

Audited on Mar 26, 2026

No findings