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

async-local

v1.0.1

Published

async_hooks based local storage aka thread-local concept in java

Downloads

8

Readme

async-local

codecov](https://codecov.io/gh/dimichgh/async-local) Build Status NPM Downloads Known Vulnerabilities

async_hooks based local storage aka thread-local concept in java world.

The module provides a single layer of local storage for async flow, which is more than enough for a big platform.

Install

npm install async-local -S

Usage

Express middleware example

const AsyncLocal = require('async-local');
const express = require('express');
const app = express();
app.use((req, res, next) => {
    AsyncLocal.run(next);
});

Storing/reading data

const AsyncLocal = require('async-local');
console.log(AsyncLocal.get('foo')); // >>> undefined
AsyncLocal.set('foo', 'bar'); // >>> throw error if no context is setup

AsyncLocal.run(ctx => {
    AsyncLocal.set('foo', 'bar');
    // or
    ctx.set('foo', 'bar');

    const promise = Promise.resolve();;

    AsyncLocal.run(ctx => {
        console.log(ctx.get('foo')); // >>> bar
        console.log(AsyncLocal.get('foo')); // >>> bar
        AsyncLocal.set('foo', 'qaz');
        console.log(ctx.get('foo')); // >>> qaz
        console.log(AsyncLocal.get('foo')); // >>> qaz

        // promise preserves current context for the caller
        promise.then(() => {
            console.log(ctx.get('foo')); // >>> qaz
            console.log(AsyncLocal.get('foo')); // >>> qaz
        });
    });

    // promise preserves current context for the caller
    promise.then(() => {
        console.log(ctx.get('foo')); // >>> bar
        console.log(AsyncLocal.get('foo')); // >>> bar
    });
});

Binding context

There are some edge cases when context is not preserved in async flow. For such cases, it makes sense to bind context to the function or emitter explicitly.

const EventEmitter = require('events');
const emitter = new EventEmitter();
AsyncLocal.bindEmitter(emitter);
const origFn = () => {
    console.log(AsyncLocal.get('foo')); // >>> bar
};

AsyncLocal.run(() => {
    AsyncLocal.set('foo', 'bar');
    const fn = AsyncLocal.bind(origFn);

    fn();
})