New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

async-local

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-local

async_hooks based local storage aka thread-local concept in java

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
Maintainers
1
Weekly downloads
 
Created
Source

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();
})

Keywords

FAQs

Package last updated on 26 Aug 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc