background-forever
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Background Forever

A simple utility to help you run node functions forever.

Quick Start

npm install --save background-forever
import { BackgroundForever } from 'background-forever';

const bf = new BackgroundForever(() => {
    return new Promise<void>((resolve, reject) => {
        console.log('hello world!');
        resolve();
    });
});

bf.start();

bf.stop().then(() => {
    console.log('stopped successfully!');
}).catch((e) => {
    console.error('error occurred while stopping the function', e);
});

Events

BackgroundForever emits a number of events for you to keep track of.

Before Run

beforeRun fires each time before calling your function. The value is the run count, starting at 1.

bf.on('beforeRun', (runCount) => {
    console.log(runCount); // 1
});

Run Success

runSuccess fires after your function successfully resolves its returned promise. The value is whatever your function returns.

const bf = new BackgroundForever(() => {
    return new Promise((resolve, reject) => {
        console.log('hello world!');
        resolve('Howdy!');
    });
});

bf.on('runSuccess', (msg) => {
    console.log(msg); // Howdy!
});

bf.start();

Run Error

runError fires if your function throws an error. The value in the event is the error or Promise reject value.

const bf = new BackgroundForever(() => {
    return new Promise((resolve, reject) => {
        reject('Fail!');
    });
});

bf.on('runError', (e) => {
    console.log(e); // Fail!
});

bf.start();

Package Sidebar

Install

npm i background-forever

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

10.9 kB

Total Files

12

Last publish

Collaborators

  • briankopp