About stdlib...
We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.
The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.
When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.
To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
Invoke a function
n
times and return an array of accumulated function return values.
npm install @stdlib/utils-async-map-function
var mapFunAsync = require( '@stdlib/utils-async-map-function' );
Invokes a function n
times and returns an array
of accumulated function return values.
function fcn( i, next ) {
var t = 300 - (i*50);
setTimeout( onTimeout, t );
function onTimeout() {
console.log( i );
next( null, i );
}
}
function done( error, arr ) {
if ( error ) {
throw error;
}
console.log( arr );
}
mapFunAsync( fcn, 5, done );
/* =>
4
3
2
1
0
[ 0, 1, 2, 3, 4 ]
*/
For each iteration, the provided function is invoked with two arguments:
-
index
: invocation index (starting from zero) -
next
: callback to be invoked upon function completion
The next
callback accepts two arguments:
-
error
: error argument -
result
: function result
The function accepts the following options
:
-
limit
: the maximum number of pending invocations at any one time. Default:infinity
. -
series
:boolean
indicating whether to sequentially invokefcn
. Iftrue
, the function setsoptions.limit=1
. Default:false
. -
thisArg
: the execution context forfcn
.
By default, all invocations are performed concurrently, which means that the function does not guarantee completion order. To invoke a function such that only one invocation is pending at any one time, set the series
option to true
.
function fcn( i, next ) {
var t = 300 - (i*50);
setTimeout( onTimeout, t );
function onTimeout() {
console.log( i );
next( null, i );
}
}
function done( error, arr ) {
if ( error ) {
throw error;
}
console.log( arr );
}
var opts = {
'series': true
};
mapFunAsync( fcn, 5, opts, done );
/* =>
0
1
2
3
4
[ 0, 1, 2, 3, 4 ]
*/
To limit the maximum number of pending function invocations, set the limit
option.
var delays = [ 300, 250, 225, 150, 100 ];
function fcn( i, next ) {
setTimeout( onTimeout, delays[ i ] );
function onTimeout() {
console.log( i );
next( null, i );
}
}
function done( error, arr ) {
if ( error ) {
throw error;
}
console.log( arr );
}
var opts = {
'limit': 2
};
mapFunAsync( fcn, 5, opts, done );
/* =>
1
0
3
2
4
[ 0, 1, 2, 3, 4 ]
*/
To set the execution context of fcn
, set the thisArg
option.
function fcn( i, next ) {
this.count += 1;
setTimeout( onTimeout, 0 );
function onTimeout() {
next( null, i );
}
}
function done( error, arr ) {
if ( error ) {
throw error;
}
console.log( arr );
// => [ 0, 1, 2, 3, 4 ]
}
var ctx = {
'count': 0
};
var opts = {
'thisArg': ctx
};
mapFunAsync( fcn, 5, opts, done );
Returns a function
which invokes a function n
times and returns an array
of accumulated function return values.
function fcn( i, next ) {
var t = 300 - (i*50);
setTimeout( onTimeout, t );
function onTimeout() {
console.log( i );
next( null, i );
}
}
function done( error, arr ) {
if ( error ) {
throw error;
}
console.log( arr );
}
var opts = {
'series': true
};
var f = mapFunAsync.factory( opts, fcn );
f( 5, done );
/* =>
0
1
2
3
4
[ 0, 1, 2, 3, 4 ]
*/
f( 2, done );
/* =>
0
1
[ 0, 1 ]
*/
The function accepts the same options
as mapFunAsync()
.
- If a provided function calls the
next
callback with a truthyerror
argument, the function suspends execution and immediately calls thedone
callback for subsequenterror
handling. -
Neither
mapFunAsync
nor the function returned by thefactory
method guarantee asynchronous execution. To guarantee asynchrony, wrap thedone
callback in a function which either executes at the end of the current stack (e.g.,nextTick
) or during a subsequent turn of the event loop (e.g.,setImmediate
,setTimeout
).
var randu = require( '@stdlib/random-base-randu' );
var mapFunAsync = require( '@stdlib/utils-async-map-function' );
function rand( i, next ) {
var t = randu() * i;
setTimeout( onTimeout, t );
function onTimeout() {
next( null, t );
}
}
function done( error, out ) {
if ( error ) {
throw error;
}
console.log( out );
}
mapFunAsync( rand, 10, done );
-
@stdlib/utils-map-function
: invoke a function n times and return an array of accumulated function return values.
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
See LICENSE.
Copyright © 2016-2024. The Stdlib Authors.