0.0.1 • Published 10 months ago

terminatable_spleep v0.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
10 months ago

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

0.0.1

10 months ago