loopify

0.1.1 • Public • Published

loopify

Loop over a function

Usage

loopify = require("loopify");
var n = 0;
var action = function () {
    n++;
    console.log("Iteration " + n);
    if (> 4) {
        // Throw null to stop looping without rejecting
        throw null;
    }
}
// Prints
// > Iteration 1
// > Iteration 2
// > Iteration 3
// > Iteration 4
// > Done!
loopify(action).then(function () {
    console.log("Done!");
});
 
// Add a sleep period of 50ms between callbacks
loopify(50, action).then(function () {
    console.log("Done!");
});
 
action = function () {
    n++;
    console.log("Iteration " + n);
    if (> 4) {
        // Throw en error to reject the loopify promise.
        throw new Error("One loop too many.");
    }
}
 
// Prints
// > Iteration 1
// > Iteration 2
// > Iteration 3
// > Iteration 4
// > One loop too many.
loopify(action).catch(function (err) {
    console.error(err.message);
});
 
 
 

Dependencies (0)

    Dev Dependencies (2)

    Package Sidebar

    Install

    npm i loopify

    Weekly Downloads

    2

    Version

    0.1.1

    License

    MIT

    Last publish

    Collaborators

    • alxarch