npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fire-and-forgetter

v3.2.1

Published

A simple lib to handle fire and forget operations.

Downloads

491

Readme

fire-and-forgetter

Simple lib to handle fire and forget operations.

Npm Version Actions Status CodeFactor codecov Dependabot

Installation

$ npm install fire-and-forgetter --save

The Problem

In NodeJs applications performing fire and forget operations we need a way to wait until all those operations complete in order to perform a graceful application shutdown.

Also, fire and forget operations must have proper error handling in order to avoid 'unhandledRejection'.

Example with Express

const express = require('express')
const someDb = require('some-db');
const app = express();

(async function main() {

    const db = await someDb.connect();
    
    app.post('/', (req, res) => {
        res.send('Got request, now I am processing it in fire and forget mode');
        process(req.body);
    })

    async function process(payload) {
        
        // do some stuff that could take several milliseconds
        // i.e interact with another service, I/O operations, etc..
        // then finally we persist the payload.

        await db.insert(payload);
    }

    const server = app.listen(3000);

    process.on('SIGTERM', async () => {
        try {
            await new Promise(resolve => server.close(resolve));
            // above we have closed the web server, now we are closing db connection
            // however we have may some fire and forget operation still in progress
            // so close the db now won't result in a clear application shutdown
            await db.close();
            process.exit(0);
        } catch (error) {
            process.exit(1);
        }
    });
})().catch(() => {
    process.exit(1);
});

Note: this example is a simplified version of real code, the someDb is a fake package and I haven't execute this code.

The Solution

Fire and forgetter is a tiny library to keep the state of fire and forget operations.

It ensures that a catch is always added to these operations (to avoid any Unhandled Promise Rejections that could lead in memory leaks).

Finally it provides a close method that:

  1. resolves when all pending fire and forget operations have been fulfilled or rejected
  2. mark the instance as closed so no new fire an forget operations can be executed.

Example with Express

const express = require('express')
const someDb = require('some-db');
const fireAndForgetter = require('fire-and-forgetter').default;
const app = express();

(async function main() {

    const fireAndForget = fireAndForgetter();
    const db = await someDb.connect();
    
    app.post('/', (req, res) => {
        res.send('Got request, now I am processing it in fire and forget mode');
        fireAndForget(() => process(req.body));
    })

    async function process(payload) {
        
        // do some stuff that could take several milliseconds
        // i.e interact with another service, I/O operations, etc..
        // then finally we persist the payload.

        await db.insert(payload);
    }

    const server = app.listen(3000);

    process.on('SIGTERM', async () => {
        try {
            await new Promise(resolve => server.close(resolve));
            // above we have closed the web server, now before closing db connection
            // we are going to close fireAndForget so we wait until all fire and forget
            // operations have been complete and we can safetly close the database connection
            // in order to ensure a good application shutdown
            await fireAndForget.close();
            await db.close();
            process.exit(0);
        } catch (error) {
            process.exit(1);
        }
    });
})().catch(() => {
    process.exit(1);
});

API

Please check tests in order to get more information about the fire and forgetter API.

License

MIT