- Notifications
You must be signed in to change notification settings - Fork8
Minimal async jobs utility library, with streams support
License
alexindigo/asynckit
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Minimal async jobs utility library, with streams support.
AsyncKit provides harness forparallel
andserial
iterators over list of items represented by arrays or objects.Optionally it accepts abort function (should be synchronously return by iterator for each item), and terminates left over jobs upon an error event. For specific iteration order built-in (ascending
anddescending
) and custom sort helpers also supported, viaasynckit.serialOrdered
method.
It ensures async operations to keep behavior more stable and preventMaximum call stack size exceeded
errors, from sync iterators.
compression | size |
---|---|
asynckit.js | 12.34 kB |
asynckit.min.js | 4.11 kB |
asynckit.min.js.gz | 1.47 kB |
$ npm install --save asynckit
Runs iterator over provided array in parallel. Stores output in theresult
array,on the matching positions. In unlikely event of an error from one of the jobs,will terminate rest of the active jobs (if abort function is provided)and return error along with salvaged data to the main callback function.
varparallel=require('asynckit').parallel,assert=require('assert');varsource=[1,1,4,16,64,32,8,2],expectedResult=[2,2,8,32,128,64,16,4],expectedTarget=[1,1,2,4,8,16,32,64],target=[];parallel(source,asyncJob,function(err,result){assert.deepEqual(result,expectedResult);assert.deepEqual(target,expectedTarget);});// async job accepts one element from the array// and a callback functionfunctionasyncJob(item,cb){// different delays (in ms) per itemvardelay=item*25;// pretend different jobs take different time to finish// and not in consequential ordervartimeoutId=setTimeout(function(){target.push(item);cb(null,item*2);},delay);// allow to cancel "leftover" jobs upon error// return function, invoking of which will abort this jobreturnclearTimeout.bind(null,timeoutId);}
More examples could be found intest/test-parallel-array.js.
Also it supports named jobs, listed via object.
varparallel=require('asynckit/parallel'),assert=require('assert');varsource={first:1,one:1,four:4,sixteen:16,sixtyFour:64,thirtyTwo:32,eight:8,two:2},expectedResult={first:2,one:2,four:8,sixteen:32,sixtyFour:128,thirtyTwo:64,eight:16,two:4},expectedTarget=[1,1,2,4,8,16,32,64],expectedKeys=['first','one','two','four','eight','sixteen','thirtyTwo','sixtyFour'],target=[],keys=[];parallel(source,asyncJob,function(err,result){assert.deepEqual(result,expectedResult);assert.deepEqual(target,expectedTarget);assert.deepEqual(keys,expectedKeys);});// supports full value, key, callback (shortcut) interfacefunctionasyncJob(item,key,cb){// different delays (in ms) per itemvardelay=item*25;// pretend different jobs take different time to finish// and not in consequential ordervartimeoutId=setTimeout(function(){keys.push(key);target.push(item);cb(null,item*2);},delay);// allow to cancel "leftover" jobs upon error// return function, invoking of which will abort this jobreturnclearTimeout.bind(null,timeoutId);}
More examples could be found intest/test-parallel-object.js.
Runs iterator over provided array sequentially. Stores output in theresult
array,on the matching positions. In unlikely event of an error from one of the jobs,will not proceed to the rest of the items in the listand return error along with salvaged data to the main callback function.
varserial=require('asynckit/serial'),assert=require('assert');varsource=[1,1,4,16,64,32,8,2],expectedResult=[2,2,8,32,128,64,16,4],expectedTarget=[0,1,2,3,4,5,6,7],target=[];serial(source,asyncJob,function(err,result){assert.deepEqual(result,expectedResult);assert.deepEqual(target,expectedTarget);});// extended interface (item, key, callback)// also supported for arraysfunctionasyncJob(item,key,cb){target.push(key);// it will be automatically made async// even it iterator "returns" in the same event loopcb(null,item*2);}
More examples could be found intest/test-serial-array.js.
Also it supports named jobs, listed via object.
varserial=require('asynckit').serial,assert=require('assert');varsource=[1,1,4,16,64,32,8,2],expectedResult=[2,2,8,32,128,64,16,4],expectedTarget=[0,1,2,3,4,5,6,7],target=[];varsource={first:1,one:1,four:4,sixteen:16,sixtyFour:64,thirtyTwo:32,eight:8,two:2},expectedResult={first:2,one:2,four:8,sixteen:32,sixtyFour:128,thirtyTwo:64,eight:16,two:4},expectedTarget=[1,1,4,16,64,32,8,2],target=[];serial(source,asyncJob,function(err,result){assert.deepEqual(result,expectedResult);assert.deepEqual(target,expectedTarget);});// shortcut interface (item, callback)// works for object as well as for the arraysfunctionasyncJob(item,cb){target.push(item);// it will be automatically made async// even it iterator "returns" in the same event loopcb(null,item*2);}
More examples could be found intest/test-serial-object.js.
Note: Sinceobject is anunordered collection of properties,it may produce unexpected results with sequential iterations.Whenever order of the jobs' execution is important please useserialOrdered
method.
TBD
For examplecompare-property package.
TBD
More examples can be found intest folder.
Or open anissue with questions and/or suggestions.
AsyncKit is licensed under the MIT license.
About
Minimal async jobs utility library, with streams support