启动 
最小的 Aync 任务工具库, 并有流支持 .
AsyncKit 提供了对 parallel 财务报告和审定财务报表 serial 以数组或对象为代表的项目列表上的显示器.
它可选择接受中止函数(每个项目应同步返回),并在出错事件时终止遗留的工作。 内置特定迭代顺序(ascending 财务报告和审定财务报表 descending)和自定义排序的帮助者也支持,通过 asynckit.serialOrdered 方法.
它可以确保同步操作,使行为更加稳定并预防 Maximum call stack size exceeded 错误,从同步的延时器.
| 压缩 | 大小 |
|---|---|
| asynckit.js (英语) | 12.34千贝 |
| (原始内容存档于2013-10-12). asynckit.min.js | 4.11千贝 |
| (原始内容存档于2013-10-12). asynckit.min.js.gz | 1.47千贝 |
安装
$ npm install --save asynckit
实例
平行任务
平行运行所提供数组的浏览器 。 存储输出于 result 阵列,
在匹配位置上。 如果其中之一出错的可能性不大,
将终止其它活动任务( 如果提供了中止功能)
并返回错误和已保存的数据到主调用函数.
输入阵列
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);
}
可在 试验/试验-平行-阵列.js。 。 。 .
输入对象
它还支持通过对象列出的命名任务.
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);
}
可在 测试/测试-平行-对象.js。 。 。 .
串行任务
依次运行已提供的数组。 存储输出于 result 阵列,
在匹配位置上。 如果其中之一出错的可能性不大,
将不再处理清单上的其他项目
并返回错误和已保存的数据到主调用函数.
输入阵列
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);
}
可在 测试/测试-序列-阵列.js。 。 。 .
输入对象
它还支持通过对象列出的命名任务.
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);
}
可在 测试/测试-连续-对象.js。 。 。 .
注:自 对象 是一个 未订购 收集属性,
它可能会产生出出乎意料的结果,并依次重复.
当任务执行的顺序很重要时请使用 serialOrdered 方法.
顺序序列
传统发展
举个例子 比较财产 软件包.
流线接口
传统发展
想知道更多吗?
更多实例可见于 测试文件夹。 。 。 .
或者打开一个 问题 提出问题和/或建议.
许可证
AsyncKit获得麻省理工学院许可.