terminatable_spleep

0.0.1 • Public • Published

Terminatable sleep

Terminatable sleep for nodejs apps

How to use:

const {terminator, terminatableSleep} = require("ternimatable_sleep");

// example
function closeConnection() {
    return new Promise(resolve => {
        setTimeout(() => {
            console.log('Close connection');
            resolve();
        }, 500);
    });
}

terminator.init({
    beforeExit: async () => {
        console.log('Before exit');

        await closeConnection();

        console.log('Connection closed');
    }
});

(async () => {

    while (true) {

        console.log('Working...');

        await terminatableSleep(15000, cancel => {
            // Call the cancel() function to cancel the sleep

            terminator.onTerminate(cancel);
        });

        // or just use await terminatableSleep(15000, terminator.onTerminate);
        
        if (terminator.isTerminating()) {
            break;
        }
    }

})();

Run your program and then hit CTRL+C

Readme

Keywords

Package Sidebar

Install

npm i terminatable_spleep

Weekly Downloads

1

Version

0.0.1

License

ISC

Unpacked Size

4.34 kB

Total Files

5

Last publish

Collaborators

  • nicklasos