run-async-gen
Utility function for running asynchronous JavaScript generators
Install
npm install run-async-gen --save
API
runAsyncGen(iter, cb)
iter
iterator created byasyncGen
cb(err, result)
callback is called whenasynGen
throw an exceptionerr
or return a valueresult
asyncGen conventions
Asynchronous generator should yield asynchronous function with one callback parameter asyncFunc(cb)
, this convention follows most of Node.js API. If your asyncFunc
requires more than one parameter, you can bind this parameters using lodash utility functions, such as bind
, partial
and partialRight
. The provided callback cb(err, result)
should be called with one of two arguments err
or result
. If err
is not null
or undefined
yield
statement will throw exception with this value, it can be handled using standatrd try-catch
statements. If no error
, yield
statement will return result
.
To run a number of asynchronous functions in parallel, you can pass to yield
statement an array of asynchronous functions [asyncFn1, asyncFun2]
, then yield will return their results as an array in same order ['result1', 'result2']
.
Example
'use strict'; const _ = ;const fs = ;const runAsyncGen = ; { console; for let i = 0; i < times; i++ console; console; _; console; if Math > 05 throw 'Error :('; return 'test result';} ; ;