1.1.0 • Published 7 months ago

loop-controller v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Loop Controller

Controls serial execution of promises

Installation

npm i loop-controller

Usage

import { LoopController } from "loop-controller";

//create a loop controller, that runs five iterations.
//It starts execution automatically.

const lc = new LoopController({
  initialArray: [1, 2, 3, 4, 5],
  makePromise: (index) => {
    console.log("iteration: " + index);
    return Promise.resolve();
  },
});

// at any point call

lc.pause();
lc.resume();
lc.setArray();
lc.prependArray();

testing

The underlying waiter is exposed, to it's possible to 'hook' into the loop controller's transitions.

//await first iteration finishes, and pause it.

lc.waiter.await("iteration-done").then(() => {
  //LoopController will receive a pause command in a certain state.
  lc.pause();
});

This way we can 'inject' actions when LoopController makes state transitions or emit events. It's a powerful way to do white-box testing.

Known issues

Related work

License

MIT