### Install asynckit Source: https://github.com/alexindigo/asynckit/blob/master/README.md Install the package via npm. ```shell $ npm install --save asynckit ``` -------------------------------- ### Run Parallel Jobs with Object Input Source: https://github.com/alexindigo/asynckit/blob/master/README.md Executes named jobs defined in an object, supporting a full value, key, and callback interface. ```javascript var parallel = require('asynckit/parallel') , assert = require('assert') ; var source = { 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) interface function asyncJob(item, key, cb) { // different delays (in ms) per item var delay = item * 25; // pretend different jobs take different time to finish // and not in consequential order var timeoutId = 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 job return clearTimeout.bind(null, timeoutId); } ``` -------------------------------- ### Run serial jobs over an object Source: https://github.com/alexindigo/asynckit/blob/master/README.md Executes an asynchronous job for each property in an object sequentially. Uses a shortcut interface where the iterator receives the item and a callback. ```javascript var serial = require('asynckit').serial , assert = require('assert') ; var source = [ 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 = [] ; var source = { 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 arrays function asyncJob(item, cb) { target.push(item); // it will be automatically made async // even it iterator "returns" in the same event loop cb(null, item * 2); } ``` -------------------------------- ### serial(source, iterator, callback) Source: https://github.com/alexindigo/asynckit/blob/master/README.md Runs an iterator function over a provided array or object sequentially. The results are stored in a result object or array matching the input structure. ```APIDOC ## serial(source, iterator, callback) ### Description Runs an iterator function over a provided array or object sequentially. If an error occurs, it stops processing and returns the error along with any salvaged data. ### Parameters - **source** (Array|Object) - Required - The collection to iterate over. - **iterator** (Function) - Required - The function to execute for each item. Supports signatures (item, callback) or (item, key, callback). - **callback** (Function) - Required - The final callback function receiving (err, result). ### Usage Example ```javascript var serial = require('asynckit/serial'); serial([1, 2], function(item, cb) { cb(null, item * 2); }, function(err, result) { // result is [2, 4] }); ``` ``` -------------------------------- ### Run Parallel Jobs with Array Input Source: https://github.com/alexindigo/asynckit/blob/master/README.md Executes an iterator over an array in parallel, storing results in a matching array. The iterator returns an abort function to handle job cancellation on error. ```javascript var parallel = require('asynckit').parallel , assert = require('assert') ; var source = [ 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 function function asyncJob(item, cb) { // different delays (in ms) per item var delay = item * 25; // pretend different jobs take different time to finish // and not in consequential order var timeoutId = 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 job return clearTimeout.bind(null, timeoutId); } ``` -------------------------------- ### Run serial jobs over an array Source: https://github.com/alexindigo/asynckit/blob/master/README.md Executes an asynchronous job for each item in an array sequentially. The iterator receives the item, key, and a callback. ```javascript var serial = require('asynckit/serial') , assert = require('assert') ; var source = [ 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 arrays function asyncJob(item, key, cb) { target.push(key); // it will be automatically made async // even it iterator "returns" in the same event loop cb(null, item * 2); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.