howdo
异步流程控制。
入门指引:http://FrontEndDev.org/column/howdo-introduction/
FEATURES
- nodejs(all)、browser(IE6/7/8/9/10/11、chrome、firefox)通用
- 注重约定
- 同步、异步任务都支持
- 支持顺序串行任务
- 支持顺序并行任务
- 自动实例化
- 链式操作
INSTALL
nodejs
npm install -S howdo
var howdow = require('howdo');
browser
CMD
defined(function(require, exports, module){
var howdo = require('./path/to/howdo.js');
});
全局
<script src="/path/to/howdo.js"></script>
<script>
</script>
API
遵守这个约定,回调的第一个参数为错误对象。
// err 为 null 或 undefined 时表示回调成功
// 否则为回调失败
callback(err, ...);
#task
分配单个任务,链式
howdo
.task(function (next) {
next(null, 1, 2, 3);
})
.task(function (next, data1, data2, data3) {
next(null, data1 + data2 + data3);
})
.try(function(data){
})
.catch(function (err) {
})
.follow();
howdo
.task(function (done) {
done(null, 1, 2, 3);
})
.task(function (done) {
done(null, 4);
})
.try(function(data1, data2, data3, data4){
})
.catch(function(err){
})
.follow();
#each
循环分配任务,链式
var list = [1, 2, 3, 4];
howdo
.each(list, function (key, val, next, data) {
next(null, val);
})
.try(function(data){
})
.catch(function(err){
})
.follow();
howdo
.each(list, function (key, val, done) {
done(null, val);
})
.try(function(data1, data2, data3, data4){
})
.catch(function(err){
})
.together();
#follow
顺序串行任务,链式
follow
用来收集任务结果,如其字面意思,表示多个任务是顺序串行执行的。
#together
顺序并行任务,链式
together
也是用来收集任务结果,如其字面意思,表示多个任务是顺序并行执行的。
try
任务成功回调,链式
.try(function(arg0, arg1, ...){
// ...
})
catch
任务失败回调,链式
.try(function(err){
// 任务失败的回调
})
VERSION
2.0.x
v1.1.4
v1.1.1
v1.1.0
- 兼容到IE6、chrome、firefox
- 兼容到nodejs
v0.0.1