轻量级JavaScript逻辑框架 – tas
Tas.js
Tas是一个轻量级JavaScript逻辑框架,无依赖。
便于使用
学习零成本。使用JavaScript的基础语法,你可以写出绝大多数的任务。
无需以回调的给事编写任务,只需按照逻辑顺序
通过返回或这个将数据传递给下一个函数或任务
安装
Node.js:
$ npm install tas --save
Web / RequireJS:
下载Tas.js 或 Tas.min.js .
入门示例
同步任务
tas("My first tasks", {
t1: function () {
return [1, 2, 3];
},
t2: function(a, b, c){
console.log(a, b, c); // 1 2 3
return [4, 5, 6];
}
});
tas("My Second tasks", {
t3: {
t4: function (a, b, c) {
console.log(a, b, c); // 4 5 6
return [7, 8, 9];
}
},
t5: {
t6: function (a, b, c) {
console.log(a, b, c); // 7 8 9
}
}
});
异步任务
var a = 0;
tas.await(function(){
a ++; // 1
setTimeout(function(){
a ++; // 2
tas.next();
}, 1000);
});
// This task is executed after the previous async task execution is completed.
// This proves that all tasks are executed in the order we have written.
tas(function(){
a ++ ; // 3
console.log(a); // 3
})
Promise
tas.promise(function(){
// Use this.done to pass the data
ajax.get('https://api.github.com', this.done);
});
tas(function (err, data) {
if (err) {
console.log(err);
}
else {
console.log(data);
}
});
模块化
a.js (dependency)
var tas = require('tas');
var a = 0;
tas.await(function(){
a ++; // 1
setTimeout(function(){
a ++; // 2
tas.next();
}, 1000);
});
tas(function(){
a ++; // 3
});
module.exports = {
get: function(){
return a; // 3
}
};
b.js (dependency)
var tas = require('tas');
var a = 2;
tas.await(function(){
a ++; // 3
setTimeout(function(){
a ++; // 4
tas.next();
}, 500);
});
tas(function(){
a ++; // 5
})
module.exports = {
get: function(){
return a; // 5
}
};
calc.js (depends on a.js and b.js)
var tas = require('tas');
// The tasks in a.js and b.js are executed in the order we have written.
var ma = require('./a');
var mb = require('./b');
// This task will be executed after all tasks are completed.
tas(function(){
var a = ma.get(); // 3
var b = mb.get(); // 5
console.log(a + b); // 8
});