epic-sync-loops

A great library that will convert your Asynchronous JavaScript code into Synchronous JavaScript!!!

Usage no npm install needed!

<script type="module">
  import epicSyncLoops from 'https://cdn.skypack.dev/epic-sync-loops';
</script>

README

Epic Synchronous Loops

A great library that will convert your Asynchronous JavaScript code into Synchronous JavaScript!!!

Installation

$ npm i epic-sync-loops

Usage


const epicSyncLoops = require("epic-sync-loops");

//Simple and instant implementation
const loop1 = new epicSyncLoops.epicSyncLoops((i) => {
    setTimeout(() => {
        console.log("Loop 1 - " + i);
        if (i > 4) {
            loop1.end();
        } else {
            loop1.next();
        }
    }, 1000);
});

//Expected Results
/**
 * Loop 1 - 0
 * Loop 1 - 1
 * Loop 1 - 2
 * Loop 1 - 3
 * Loop 1 - 4
 * Loop 1 - 5
 *
 */

//Random Top To Bottom Execution
const loop1 = new epicSyncLoops.epicSyncLoops();
const loop2 = new epicSyncLoops.epicSyncLoops();
const loop3 = new epicSyncLoops.epicSyncLoops();

loop1.loop((i) => {
    setTimeout(() => {
        console.log("Loop 1 - " + i);
        if (i > 4) {
            loop1.end();
        } else {
            loop1.next();
        }
    }, 1000);
}).after(loop3);

loop2.loop((i) => {
    setTimeout(() => {
        console.log("Loop 2 - " + i);
        if (i > 4) {
            loop2.end();
        } else {
            loop2.next();
        }
    }, 1000);
}).after(loop1);

loop3.loop((i) => {
    setTimeout(() => {
        console.log("Loop 3 - " + i);
        if (i > 4) {
            loop3.end();
        } else {
            loop3.next();
        }
    }, 1000);
});

//Expected Results
/**
 * Loop 3 - 0
 * Loop 3 - 1
 * Loop 3 - 2
 * Loop 3 - 3
 * Loop 3 - 4
 * Loop 3 - 5
 * Loop 1 - 0
 * Loop 1 - 1
 * Loop 1 - 2
 * Loop 1 - 3
 * Loop 1 - 4
 * Loop 1 - 5
 * Loop 2 - 0
 * Loop 2 - 1
 * Loop 2 - 2
 * Loop 2 - 3
 * Loop 2 - 4
 * Loop 2 - 5
 */

Construct Parameters

  • callBack - Called on each execution. (Carries the Index of loop in first Parameter).
  • speed - Control the speed of loop for each call.

Methods

  • loop() - Can be used instead of constructor callBack.
  • next() - Execute Loop's Next callBack. (Loop will never go next until this method is called).
  • end() - End The Loop.
  • after() - Executed Seconde Loop After First One. (This feature Allows you to execute code with your own Order).