1.0.5 • Published 8 years ago

asynciterators v1.0.5

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

asynciterators

A collection of async iteration methods for Javascript/Node.

Installation

$ npm install asynciterators --save

Examples

There's examples in the documentation in the source code for each of the methods, but here they are for your viewing pleasure:

asyncForEach

Takes an array and iterates through the collection asynchronously by calling the provided callback function for every time next() is called. When finished, the done() callback will be called if provided. If the loop should be broken, it's possible to call done() whenever you like. By default, it ensures that each iteration is called at the end of each call stack and thus won't block rendering. You can disable this behavior with the fourth argument:

asyncForEach(Array<T>, function(item: T, index: number, next: function, ?done: function), ?onDone: function, ?blockRenderingThread: boolean)

Example:

const arr = ["Foo", "Bar", "Baz"];
 
asyncForEach(arr, (item, index, next, done) => {
      awaitCoolAsyncStringOperation(item)
          .then(next);
}, () => {
      //Called when all collection items has been iterated over or done() is explicitly called.
});

asyncUntil

Will execute the given callback asynchronously at the end of each call stack for every time next() is called. In order to stop execution, done() should be explicitly called.

asyncUntil(function(next: function, done: function), ?onDone: function);

Example:

asyncUntil((next, done) => {
    if (somethingIsReady) done();
    else awaitSomeStuff()
            .then(next)
}, () => {
    //Called when done() is called.
});

parallel

Will take an array of functions and execute them in parallel and call done() if given when all function has finished executing (asynchronously).

parallel([...function(onDone: function)])

Example:

parallel([
             (done) => {
                 awaitReady()
                   .then(done)
                   .catch(done)
             },
             (done) => {
                 awaitSomeThingElse()
                   .then(done)
                   .catch(done)
             }
         ], (error, data) => {
             if (error) throw new Error("Error, wow!!");
             console.log("Data ready!!!");
         });

Usage

Simply do npm install asynciterators --save;

And then all that's left is to import what you want:

import {asyncForEach} from "asynciterators";

or

const asyncForEach = require("asynciterators).asyncForEach;

License

Copyright (c) 2016 Dlmma IVS. Released under the MIT license.

1.0.5

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago