async-iteration
Enables asynchronous iteration on sets of deferred data.
Table of Contents
Installation
As a node package
npm install async-iteration
As a standalone source
Download uncompressed or minified source.
This NPM package is transpiled using Babel to support ES5 environments, but the linked standalone sources aren't. You'll need to transpile them yourself in your project if support of older environments is needed.
Examples
Basic example
With es8 async functions
const AsyncIteration = ; const iteration = ; // Iterate asynchronouslyasync { for const iter = iteration; await ; console; }; // Wait for completionasync { const values = await iteration; console; console;};
Outputs:
3
4
2
completed!
[3, 4, 2]
With es6 promises only
var AsyncIteration = ; var iteration = ; // Iterate asynchronouslyvar iter = iteration;; // Wait for completioniteration;
Outputs:
3
4
2
completed!
[3, 4, 2]
mz
Advanced example: reading files usingWith es8 async functions
const readFile = ;const AsyncIteration = ; const files = 'path/to/file1' 'path/to/file2' 'path/to/file3'; const results = ; async { // Iterate asynchronously through results for const iter = results; await ; const file content = iterresult; console; console; // Note: It is possible to handle eventual errors, `iter` doesn't silence // errors, it will throw if readFile throws.};
Possible output:
path/to/file2
... (content of file2)
path/to/file1
... (content of file1)
path/to/file3
... (content of file3)
With es6 promises only
var readFile = readFile;var AsyncIteration = ; var files = 'path/to/file1' 'path/to/file2' 'path/to/file3'; var results = ; // Iterate asynchronously through resultsvar iter = results;;// Note: It is possible to handle eventual errors with Promise.prototype.catch,// `iter` doesn't silence errors, it will reject if readFile rejects.
Possible output:
path/to/file2
... (content of file2)
path/to/file1
... (content of file1)
path/to/file3
... (content of file3)
API
-
AsyncIteration
factoryvar iteration = AsyncIteration(asyncFn);
Factory function that creates and returns a plain object used for asynchronous iteration.
-
asyncFn
async (include) => { ... }
ES8 async function or function that returns an ES6 promise. It will be invoked immediately with an
include
parameter.-
include
include(val);
Function passed as parameter to
asyncFn
. It accepts one argument. It can be invoked beforeasyncFn
resolves to resolve the next deferred value. It will throw an error if it is invoked afterasyncFn
resolves.
-
-
iteration
var iter = iteration.iterate();
var promise = iteration.promise();
Plain object used for iterating asynchronously through the values resolved by
asyncFn
.-
iter
for (const iter = iteration.iterate(); await iter();) { const { result } = iter; ... }
Function that on its Nth invocation will promise the Nth value inclusion by
asyncFn
. It always returns a promise that always resolves a boolean telling if the iteration has concluded or not. It has aresult
property that will contain the Nth included value on resultion of its Nth returned promise. IfasyncFn
rejects at some point, the returned promises after after the included values will all reject with the same error ofasyncFn
. -
promise
const values = await promise;
Promise that resolves an array of all included values after
asyncFn
resolves. IfasyncFn
rejects, the promise will reject the same error ofasyncFn
.
-
-
License
MIT