npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mildloop

v1.3.0

Published

A simple helper library for async function's loop execution.

Downloads

13

Readme

mildloop

A simple helper library for async function's loop execution. It allows you to execute your async function logic repeatedly, passing any parameter to every executions of a function.

Install

npm install --save mildloop

Basic Usage

const mildloop = require("mildloop");

// mildloop options:
var options = {
  suspend: true, // suspend if any error occurred. Defaults to false.
}

var params = [...]; // Array of param which is pass to each function

// Let's use mildloop!
mildloop({ params, fn: asyncfn, options }, (errors, results, status) => {
  console.log("status:", status);
  console.log("errors:", errors);
  console.log("results:", results);
});

Options

| Option | Type | Detail | Default | |:-----------|:------------|:------------|:------------| | suspend | Boolean | If set true, when any error occurred in the middle of loop, breaks execution loop. | false | | interval | Number | Milliseconds of execution interval. Only the first function is executed immediately without any interval. | 0 | | condition | Function | A function which returns boolean is used as a condition to exit loop. | null |

Example

const mildloop = require("mildloop");

//
// prepare async function & thier param. it's up to you:
//

// params to be passed to each executing functions:
var params = ["a", "b", "c", "d"];

// async function you want to execute repeatedly:
function asyncfn(s, cb) {
  setTimeout(() => {
    if (typeof s == "string")
      return cb && cb(null, s.toUpperCase());
    else
      return cb && cb(Error(s + " must be a string"));
  }, 0);
}


//
// Ok, loop them:
// sets `suspend:true`. It means async loop is suspended if any error occurred.
//

// mildloop options:
var options = {
  suspend: true, // suspend if any error occurred. Defaults to false.
}

// Let's use mildloop!
const instance = mildloop({ params, fn: asyncfn, options }, (errors, results, status) => {
  console.log("status:", status);
  console.log("errors:", errors);
  console.log("results:", results);
});

// If you want to stop the loop, you can stop it by yourself in the middle:
// instance.stop(); // returned `status` on the callback will be set as "terminated"

/*
console log:

status: finished
errors: null
results: [ 'A', 'B', 'C', 'D' ]
*/

mildloop callback function returns (errors, results, status).

  • error: Errors (array) of each function. If all function returned no error, errors is null but not array.
  • results: Always array object. Each element is a result of each async function.
  • status: "finished", "suspended", "terminated", or "condition_matched".
    • If you set option.suspend: true and any error occurred, status is "suspended".
    • If you stop() the loop by yourself, "terminated" is set.
    • If you set condition option and it is matched, the execution loop is suspended and "condition_matched" is set.

If you need more examples, please check example/.