Socket
Socket
Sign inDemoInstall

@bucuo/async-control

Package Overview
Dependencies
2
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @bucuo/async-control

Help control parallel executions of async tasks.


Version published
Maintainers
1
Install size
30.0 kB
Created

Readme

Source

Async Control

Help control parallel executions of async tasks/functions.

Install

npm i @bucuo/async-control

Usage

Wrappers

Wrappers are composable high order functions which provide various async control options.

const {
  returnError,
  retry,
  limitRate,
  limitParallel,
  expoBackoff
} = require("@bucuo/async-control");

const wrappers = [
  returnError(), // returning error instead of throwing
  retry({
    maxRetries: 3,
    backoff: expoBackoff({
      factor: 1000, // milliseconds
      base: 3, // defaults to 2
      min: 100, // milliseconds, defaults to 0
      max: 1000 // milliseconds, defaults to Infinity
    })
    // backoff: constBackoff(...)
    // backoff: linearBackoff(...)
  }),
  limitRate({
    count: 100,
    interval: 1000 // milliseconds
  }),
  limitParallel(500)
];

Run

You can build a run function to run many tasks (async functions without arg) with controls.

const { buildRun } = require("@bucuo/async-control");

const wrappers = ...

const tasks = ... // async function without arg

const run = buildRun(wrappers);
// const run = buildRun(...wrappers); // another api format

const results = await run(tasks);
// const results = await run(...tasks); // another api format

Wrap

You can wrap an existing async function to get a controlled version of it.

const { buildWrap } = require("@bucuo/async-control");

const wrappers = ...

const wrap = buildWrap(wrappers);
// const wrap = buildWrap(...wrappers);

const myFetch = wrap(fetch);

const response = await fetch("https://github.com/bucuo-js");

API

Function Builders:

  • buildRun
  • buildWrap

Wrappers:

Retry Backoffs:

Errors:

Note

Note about buildWrap

If you use the same wrap functions to wrap multiple target functions, the wrapped functions will share the same suite of wrappers (so the invocations of them would be controlled in the same limit queue, etc).

This logic is correct and intended, but should be put emphasis on to avoid misunderstandings.

If you need the wrapped function to be controlled separately, make sure build separate a suit of wrappers for each of them.

License

MIT

Keywords

FAQs

Last updated on 18 May 2019

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