aloop

Tiny & async looping in javascript

Stats

StarsIssuesVersionUpdatedCreatedSize
aloop
001.0.63 months ago3 months agoMinified + gzip package size for aloop in KB

Readme

A(sync)loop - Tiny, async looping in javascript

Motivation

Have you ever wanted to write a back/front end function that needs to iterate over a large array, but you don't want to block the event loop?

Now you can:

How to use

import aloop from "aloop";

async function foo() {
    // returns a Promise
    await aloop(
        // starting number
        0,
        // when to end, number,
        1000,
        // callback for each iteration with the count as param
        i => console.log(i)
    );

    // e.g loop a 1000 times 
    // and console log the current count
    aloop(0, 1000, (i) => {
        console.log(i);
    });
};

Tests

(async () => {
    const million = 1000000;

    let count = 0;

    // we can use await here also !
    const first = aloop(0, million, i => {
        count += i;
    }).then(() => console.log("1 finish"));

    const second = aloop(0, million, i => {
        count += i;
    }).then(() => console.log("2 finish"));

    const third = aloop(0, million, i => {
        count += i;
    }).then(() => console.log("3 finish"));

    Promise.allSettled([first, second, third]).then(() => {
        console.log("We're finished", count);
    });

    console.log("I got called first !!! 🤯");

    // /$ ts-node test.ts
    // Starting
    // I got called first !!! 🤯
    // 1 finish
    // 2 finish
    // 3 finish
    // We're finished 1499998500000

})();

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.