Accumulate values asynchronously.
When looping through values in an array to perform async operations, you may be using boilerplate like this:
But usually more boilerplate is necessary. The function is called frequently and to avoid exeeding stack limits a setTimeout
. Some logic is added to throw an error when the given array is invalid. The order of array traversal is changed. With more boilerplate the role of the function is harder to understand.
accumasync
methods share the same implementation and interface details. The following parameters are passed to accumulator methods:
-
startvals= Array | Object | Number
target element for the animation
-
accumvals= *
the reference to this value is passed to each callback function
it may be redefined with the accumulated values
-
fn= Function
function is called for each item and the body of it may perform an async operation to obtain new values
the fn function is called like this:
fn(elem, index, accumvals, nextfn)
-
exitfn= Function
function is called at the end of traversal and may be called to prematurely end traversal
the exitfn function is called like this:
exitfn(err, accumvals)
accumasync.arr( arr, accum, fn, exitfn )
traverse array elements in reverse order
accumasync.arr(arr, [], function (elem, index, accumarr, next) {
requestDataForElem(elem, function (err, res) {
if (err) return next(err);
accumarr.push(res);
next(null, accumarr);
});
}, exitfn);
accumasync.arrf( arr, accum, fn, exitfn )
traverse array elements in forward order
accumasync.arrf(arr, [], function (elem, index, accumarr, next) {
requestDataForElem(elem, function (err, res) {
if (err) return next(err);
accumarr.push(res);
next(null, accumarr);
});
}, exitfn);
accumasync.obj( arr, accum, fn, exitfn )
traverse object keys in reverse order
accumasync.obj(obj, [], function (elem, index, accumarr, next) {
requestDataForElem(obj[elem], function (err, res) {
if (err) return next(err);
accumarr.push(res);
next(null, accumarr);
});
}, exitfn);
accumasync.objf( arr, accum, fn, exitfn )
traverse object keys in forward order
accumasync.obj(obj, [], function (elem, index, accumarr, next) {
requestDataForElem(obj[elem], function (err, res) {
if (err) return next(err);
accumarr.push(res);
next(null, accumarr);
});
}, exitfn);
accumasync.num( arr, accum, fn, exitfn )
traverse numbers in reverse order
accumasync.num(num, [], function (elem, index, accumarr, next) {
requestDataForElem(num, function (err, res) {
if (err) return next(err);
accumarr.push(res);
next(null, accumarr);
});
}, exitfn);
accumasync.numf( arr, accum, fn, exitfn )
traverse numbers in forward order
accumasync.numf(num, [], function (elem, index, accumarr, next) {
requestDataForElem(num, function (err, res) {
if (err) return next(err);
accumarr.push(res);
next(null, accumarr);
});
}, exitfn);
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.