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

evented-async-loop

v0.1.1

Published

Loop through asynchronous code with events

Downloads

210

Readme

##evented-async-loop Build Status

Loop through asynchronous code with events

Installation

npm install evented-async-loop

Usage

Check examples and test folders for more details.

Basic

'use strict';

var asyncLoop = require('../index');
var dummyArray = require('./dummyArray');


//Make an array of asynchronous functions
var arr = dummyArray(10);

var loop = asyncLoop.create(arr);

loop.on('next', function (elm, i, array) {
    elm(function() {
        console.log(i);
        loop.next();
    });
});

loop.on('done', function () {
    console.log('Done!');
});

loop.start();

Chaining

//All methods accept .next are chainable
loop.on('next', function (elm, i, array) {
    elm(function () {
        console.log(i);
        loop.next();
    });
}).on('done', function () {
    console.log('Done!');
}).start();

Exit early

loop.on('next', function (elm, i, array) {
    elm(function () {
        if (i === 5) {
            // Stop the loop early, you can pass any number of arguments to .done
            loop.done(i, 'i am Batman');
        }
        loop.next();
    });
}).on('done', function (i, msg) {
    console.log('Loop stopped at %s and %s', i, msg);
}).start();

Data propagation

// loop.start(...).on() is ok too!
loop.on('next', function (elm, i, array, arg1, arg2) { // extra arguments are appended
    elm(function () {
        // pass data to the next iteration
        loop.next(++arg1, arg2);
        // after the final iteration tha arguments are passed to 'done'
    });
}).on('done', function (arg1, arg2) {
    console.log('arg1: %s, arg2: %s', arg1, arg2);
}).start(0, 'blah'); // Pass any number of arguments

Error handling

loop.on('next', function (elm, i, array) {
    elm(function () {
        if (i === 3) {
            // Emitting an error event will not break the loop
            loop.error('Oh noez!');
        }

        if (i === 8) {
            // But if you need to break the loop and emit an error
            loop.break().error('This will stop the loop');
            // or, loop.error('This will stop the loop').break();
            // if you want to emit the error first
        }

        loop.next();
    });
}).on('done', function () {
    // This wont run
    console.log('Done!');
}).on('error', function (err) {
    console.log(err);
}).start();

##API ###Events

  • next[element, i, array, arg1, ...]
  • done[arg1, ...]
  • error[arg1, ...]

###Methods

  • .start([arg1, ...]): Start the loop.Passes arguments to the first next event
  • .next([arg1, ...]): Go to the next iteration. Passes agruments to next event. If the loop is completed done is emitted instead.
  • .done([arg1, ...]): Stop the loop early. Passes arguments to done event
  • .error([err]): Emit an error event
  • .break(): Break the loop. it wont emit done

##Test Change your working directory to the project's root, npm install to get the development dependencies and then npm test