fa = fluent/functional async
fa
is a fluent and functional async library. Inspired by async[1] and
underscore[2], it takes the functional operators and adds some modifiers:
to enable a queue depth, run to completion regardless of errors, run in
series, and add an index to the callback.
fa; fa
Install
npm install fa
Functions
For all of the following functions, if list
is a javascript object,
the iterator signature will be (value, key, cb)
instead of (element,cb)
.
(forEach)
: fa.each(list, iterator, callback)
each Iterates over a list of elements, yielding each in turn to an iterator
function. The iterator function is called with the element
and a
callback
. If an error occurs return it as the first argument to the
iterator callback. The final callback will be called when either all of
the list has been iterated over, or an error occurs. If list
is a
javascript object, the iterator signature will
be (value, key, cb)
.
fa
fa.map(list, iterator, callback)
map: As in each
, but build a new list of elements using the iterator
callback.
fa
(select)
: fa.filter(list, iterator, callback)
filter Iterate through the list, returning all values in the list that return a truthy result from the iterator. Note that the iterator callback should have only the truthy parameter, there is no error parameter.
fa
fa.reject(list, iterator, callback)
reject: The opposite of filter, rejects all values in the list that return a truth result. Again, like filter, the iterator callback should only have one parameter, there is no error parameter.
fa
(find)
: fa.detect(list, iterator, callback)
detect Returns the first value where the iterator's callback returns a truthy result.
fa
(some)
: fa.any(list, iterator, callback)
any Returns true if any of the list elements pass the iterator's truth test.
fa
(every)
: fa.all(list, iterator, callback)
all Returns true if all of the list elements pass the iterator's truth test.
faall234 { ;} { // no err param // result === false}
(foldl, inject)
: fa.reduce(list, memo, iterator, callback)
reduce Boils down a list into a single value. Memo is the initial state of the return value. Each successive call to the iterator must return the new value of memo.
fa
fa.concat(list, iterator, callback)
concat: As in map
, but concats the results of each iterator together.
fa
Note that, unless run in series, the results are not guaranteed to be in order.
Modifiers
The default behavior of the functions are:
- The list is iterated over in parallel. (unless that is
impossible, as in the case of
reduce
). - The entire list will be queued up immediately.
- If an error is returned in the iterator's callback, the operation will be terminated immediately.
Each of the modifiers alters the default behavior of each function.
fa.series().map(...)
series: Alter the function to run in series instead of parallel.
(c, queue)
: fa.concurrent(queue_depth).map(...)
concurrent Run only a specified number of operations in parallel. This is useful if your iterator function is competing over a limited resource, such as file descriptors.
fa.continue().map(...)
continue: If an error is returned from the iterator function, keep going, and collect all of the errors together. This array of errors is then passed to the final callback.
fa
fa.with_index().map(...)
with_index: Adds a loop index variable to the iterator function.
fa
Each of the modifiers can be chained together, in a fluent interface style. Or, they can be assigned and reused:
var fasc = fa;fasc;