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);
}, () => {
});
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)
}, () => {
});
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.