Socket
Socket
Sign inDemoInstall

@ansi-art/async-context

Package Overview
Dependencies
1
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @ansi-art/async-context

A construct for chainable contexts


Version published
Weekly downloads
1
Maintainers
1
Created
Weekly downloads
 

Readme

Source

@ansi-art/async-context

A construct for chainable contexts which use async functions compatible with callbacks or promises.

Usage

Making a context

Assuming we have an object with a function that takes an object and returns a transformed version of that object:

let str ='';
const myObject = {
    foo: (item, callback)=>{
        str += `foo ${str}`
        callback(null, str);
    }
};

Take that object and make a context using it:

import { asyncContext } from '@ansi-art/async-context';
const context = asyncContext(myObject);
const value = await context.foo().foo().foo();
// value === "foo foo foo"

Using kitchen-sync

kitchen-sync allows you to support both callbacks and promises with no stack fragmentation in the event callbacks are used, allowing for simpler debugging internally to a set of libraries, for example, but allowing external use of more modern idioms.

import { kitchenSync } from `@ansi-art/async-context/kitchen-sync`;
(async ()=>{
    const fn = (arg, cb){
        const callback = kitchenSync(cb);
        someFunctionThatTakesCallbacks(callback);
        return callback.return;
    }
    const result = await fn(someArgValue);
})();

That's all.

Testing

Run the es module tests to test the root modules

npm run import-test

to run the same test inside the browser:

npm run browser-test

to run the same test headless in chrome, firefox and safari:

npm run headless-browser-test

to run the same test inside docker:

npm run container-test

Run the commonjs tests against the /dist commonjs source (generated with the build-commonjs target).

npm run require-test

Development

All work is done in the .mjs files and will be transpiled on commit to commonjs and tested.

If the above tests pass, then attempt a commit which will generate .d.ts files alongside the src files and commonjs classes in dist

FAQs

Last updated on 04 Sep 2023

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc