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

async-lite

v1.0.3

Published

A tiny async library for modern environments with no dependencies, and only containing each, eachSeries, series, and parallel methods.

Downloads

227

Readme

Async-Lite.js

Build Status via Travis CI NPM version

Async-Lite is a tiny async library (1kb minified) for modern environments with no dependencies, and only containing each, eachSeries, series, and parallel methods. These methods are the ones I most often use when I need asynchronous looping or control flow, so bringing in the entire caolan/async library was overkill. You can use with Node.js or in the browser. Install it via npm install async-lite or just grab the async-lite.min.js file from this repository.

Documentation

each(arr, iterator, [callback])

Loop through an array without caring what order your array items are accessed. If any item passes an error to its callback, no other items are processed, and the main callback is immediately called with the value of the error.


var async = require('async-lite');

var myArray = [{name: "Sam", timeToWait: 200}, {name: "Bill", timeToWait: 400}, {name: "Steve", timeToWait: 600}];

function someAsyncFunction(item, callback) {

  setTimeout(function() {

    if (item.name === "Sam") {
      alert("That is a great name");
    } else {
      alert("You should change your name");
    }

    callback(null); // leave the callback empty or callback null if there are no errors

  }, item.timeToWait);

}

async.each(myArray, function(item, callback) {

  someAsyncFunction(item, function(err) {

    // callback when done
    // passing in a truthy error will stop the whole loop
    callback(err);

  });

});

The total time that passed could be as little as 600 milliseconds since all items are accessed at once. The total amount of time should be slightly longer than the slowest operation.

eachSeries(arr, iterator, [callback])

Loop through an array in order. An item will be only be accessed once the previous item is done being accessed. If any item passes an error to its callback, no other items are processed, and the main callback is immediately called with the value of the error.


var async = require('async-lite');

var myArray = [{name: "Sam", timeToWait: 200}, {name: "Bill", timeToWait: 400}, {name: "Steve", timeToWait: 600}];

function someAsyncFunction(item, callback) {

  setTimeout(function() {

    if (item.name === "Sam") {
      alert("That is a great name");
    } else {
      alert("You should change your name");
    }

    callback(null); // leave the callback empty or callback null if there are no errors

  }, item.timeToWait);

}

async.eachSeries(myArray, function(item, callback) {

  someAsyncFunction(item, function(err) {

    // callback when done
    // passing in a truthy error will stop the whole loop
    callback(err);

  });

});

The total time that passed will be at least 1200 milliseconds since Bill had to wait 200 milliseconds for Sam to be done before starting, and then Steve had to wait 400 milliseconds for Bill to be done before starting.

series(tasks, [callback])

Run each of the tasks in the array, each one running once the previous task has completed. The callback will receive an array of results in the correct order once all tasks are completed. If any task passes an error to its callback, no other tasks are run, and the main callback is immediately called with the value of the error.


var async = require('async-lite');

async.series([

  function task1(callback) {
    setTimeout(function() {
      callback(null, "task1");
    }, 600);
  },

  function task2(callback) {
    setTimeout(function() {
      callback(null, "task2");
    }, 400);
  },

  function task3(callback) {
    setTimeout(function() {
      callback(null, "task3");
    }, 200);
  }

], function(err, results) {

  // results = ["task1", "task2", "task3"];

});

The total time that passed will be at least 1200 milliseconds since task2 had to wait 600 milliseconds for task1 to be done before starting, and then task3 had to wait 400 milliseconds for task2 to be done before starting.

parallel(tasks, [callback])

Run all of the tasks simultaneously. Unlike the series method, parallel can accept an array or object. The callback will receive an array or object of results in the correct order once all tasks are completed. If any task passes an error to its callback, no other tasks are run, and the main callback is immediately called with the value of the error.


var async = require('async-lite');

async.parallel([

  function task1(callback) {
    setTimeout(function() {
      callback(null, "task1");
    }, 600);
  },

  function task2(callback) {
    setTimeout(function() {
      callback(null, "task2");
    }, 400);
  },

  function task3(callback) {
    setTimeout(function() {
      callback(null, "task3");
    }, 200);
  }

], function(err, results) {

  // results = ["task1", "task2", "task3"];

});

/*********** OR *************/

async.parallel({

  one: function(callback) {
    setTimeout(function() {
      callback(null, "task1");
    }, 600);
  },

  two: function(callback) {
    setTimeout(function() {
      callback(null, "task2");
    }, 400);
  },

  three: function(callback) {
    setTimeout(function() {
      callback(null, "task3");
    }, 200);
  }

}, function(err, results) {

  // results = {one: "task1", two: "task2", three: "task3"};

});

The total time that passed could be as little as 600 milliseconds since all tasks are processed at once. The total amount of time should be slightly longer than the slowest task.