@schamane/serial-exec
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

serialExec

CI npm version install size downloads

Serial execution of asynchronouse functions for javascript with zero dependancies.

Use this if serial execution of anychronouse code should be implemented.

Let say you will execute HTTP request with different values not in parallel, but waiting on request is done, and also be abble to break execution.

Package can be used als commonjs or esm module

Breaking API changes for v2

Note

We made api changes to package since v1.0, please use "single" tag on npm to use older version

npm install @schamane/serial-exec:single

Usage

There are two methods that can be used to serial execution of promises

  • all
  • overParams
  async all(list: wrapedFunction[]): Promise<T[]>;
  async overParams(params: any[], fn: Function): Promise<T[]>;

Usage example for all

import { all, useSerialExec } from '@schamane/serial-exec';

const delay = async (ms) => new Promise((resolve) => setTimeout(resolve, ms));

const asyncFn = useSerialExec(async (breakFn, ms) => {
  if (ms > 300) {
    return breakFn();
  }
  console.log(`fn ${ms} start`);
  await delay(ms);
  console.log(`fn ${ms} done`);
  return ms + 1;
});

await all([asyncFn(100), asyncFn(200), asyncFn(400), asyncFn(1000)]);

Usage example for overParams

import { overParams } from '@schamane/serial-exec';

const delay = async (ms) => new Promise((resolve) => setTimeout(resolve, ms));

const asyncFn = async (ms, breakFn) => {
  if (ms > 300) {
    return breakFn();
  }
  console.log(`fn ${ms} start`);
  await delay(ms);
  console.log(`fn ${ms} done`);
  return ms + 1;
};

await overParams([100, 200, 400, 1000], asyncFn);

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i @schamane/serial-exec

Weekly Downloads

0

Version

2.0.2

License

MIT

Unpacked Size

8.65 kB

Total Files

9

Last publish

Collaborators

  • schamane