Batching
Map multiple call async functions that have the same context with the earliest result returned.
Usage
Assume you have a slow API
{ console; return { ; };}
and make a batch call
{ try let results = await Promiseall ; console; catch err console; }; ;
You realize that a lot of calls have the same context, and don't want to repeat it.
slowDoubleAPI(1)
slowDoubleAPI(2)
slowDoubleAPI(1)
slowDoubleAPI(1)
slowDoubleAPI(2)
slowDoubleAPI(3)
slowDoubleAPI(1)
slowDoubleAPI(2)
slowDoubleAPI(1)
slowDoubleAPI(1)
slowDoubleAPI(2)
slowDoubleAPI(1)
slowDoubleAPI(1)
slowDoubleAPI(2)
slowDoubleAPI(3)
slowDoubleAPI(1)
slowDoubleAPI(2)
slowDoubleAPI(1)
RESULT : [ 2, 4, 2, 2, 4, 6, 2, 4, 2, 2, 4, 2, 2, 4, 6, 2, 4, 2 ]
Let batch them ...
const batch = ; { try let results = await Promiseall ; console; catch err console; }; ;
The first call slowDoubleAPI(1) will be executed, but later call slowDoubleAPI(1) before first call finish won't, just push to queue, and assign them to the first call result when it finishes.
slowDoubleAPI(1)
slowDoubleAPI(2)
slowDoubleAPI(3)
RESULT : [ 2, 4, 2, 2, 4, 6, 2, 4, 2, 2, 4, 2, 2, 4, 6, 2, 4, 2 ]
Let turn on log to view detail :
batch;
Will print :
[batch] slowDoubleAPI() start, with :
* ARGS : [1]
* THIS : undefined
slowDoubleAPI(1)
[batch] slowDoubleAPI() start, with :
* ARGS : [2]
* THIS : undefined
slowDoubleAPI(2)
[batch] slowDoubleAPI() start, with :
* ARGS : [3]
* THIS : undefined
slowDoubleAPI(3)
[batch] slowDoubleAPI() finish successful, with :
* TIME : 601ms
* ARGS : [3]
* THIS : undefined
* Callbacks : 2
* RESULT : 6
[batch] slowDoubleAPI() finish successful, with :
* TIME : 644ms
* ARGS : [1]
* THIS : undefined
* Callbacks : 10
* RESULT : 2
[batch] slowDoubleAPI() finish successful, with :
* TIME : 676ms
* ARGS : [2]
* THIS : undefined
* Callbacks : 6
* RESULT : 4
API
function
Batch(options) ⇒ Create new batch
Kind: global function
Returns: function
- batch
Param | Type | Default | Description |
---|---|---|---|
options | object |
||
[options.logStart] | boolean | function |
false |
interface : (asyncFunc : function, context : object) => void |
[options.logFinish] | boolean | function |
false |
interface : (asyncFunc : function, context : object, err : object, res : any) => void |
[options.isArgsEqual] | function |
_.isEqual |
arguments comparator |
[options.isThisEqual] | function |
_.isEqual |
this pointer comparator |
Example
const Batch = ; const custom_batch = ;
function
Batch~wrap(asyncFunc, [thisArg]) ⇒ Wrap a async function to support batching
Kind: inner method of Batch
Returns: function
- batchedAsyncFunction : (...args) => Promise
Param | Type |
---|---|
asyncFunc | function |
[thisArg] | any |
Example
// Instead of : const batch = ; let user1 = await ;let user2 = await ;let user3 = await ; // Use can wrap findOne() : const batchedFindOne = batch; // And call it : let user1 = await ;let user2 = await ;let user3 = await ;
Promise
Batch~batch(asyncFunc, args, thisArg) ⇒ Map multiple call async functions that have the same context with the earliest result returned.
Kind: inner method of Batch
Returns: Promise
- that return by first finished invoking
Param | Type |
---|---|
asyncFunc | function |
args | array |
thisArg | object |
Example
const batch = ; let store = await ; // with thisArglet store = await ;
Batch~setLog(options)
Set log options
Kind: inner method of Batch
Param | Type | Description |
---|---|---|
options | boolean | object |
turn log on or off, or customer log function |
Example
// set all logbatch; // set specific logbatch; // custom log functionbatch;