Tas.js
Tas makes the code structure clear, turning async code to sync, reducing the levels, avoiding callback hell / pyramid, is better than Promise, and can be used in Node.js and in browsers.
The tasks written by Tas do not need to use Promise / resolve / reject, generator / yield, async / await, so Tas is easier to use than Promise. And, also Tas is faster than Promise and promise libraries (Why).
Tas is a lightweight JavaScript logic framework (only 3KB gzipped), with no dependency. Tas is the abbreviation of "tasks".
Easy to Use
- Zero learning costs. Using the basic syntax of JavaScript, you can write the vast majority of tasks.
- There is no need to write the tasks in the form of a callback, just in logical order.
- Pass the data via return or this to the next function or tasks naturally.
Install
In Node.js:
$ npm install tas --save
In Web / RequireJS:
Download Tas.js or Tas.min.js.
Quick Examples
Sync Tasks
With Tas, we can turn complex logic or high-coupling logic into a set of mini-tasks. All mini-tasks are executed one by one.
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
}
}
});
Async Tasks
With Tas, we can write async code like writing sync code, easily avoiding callback hell / pyramid. All async tasks and sync tasks will be executed in the order we have written.
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
})
As Promise
We can use Tas as if using Promise. The tasks written by Tas do not need to use Promise / resolve / reject, generator / yield, async / await, so Tas is easier to use than Promise and promise libraries.
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);
}
});
Modularization
With Tas, when all dependencies (even including async module) execution are completed, the current module will get theire results. Everything becomes sync execution, no callback hell / pyramid.
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
});
Full Examples
Clone the Tas repo first:
$ cd /path/to
$ git clone https://github.com/tasjs/tas.git
For Node.js
Run the test of examples in Node.js:
$ cd /path/to/tas/examples
$ node nodejs/__tas/test.js
Or view the source code of the examples online:
Folder | Topic | Example | Test |
---|---|---|---|
1. Pass the data | 1. Hello world | Example | Test |
1. Pass the data | 2. Via return | Example | Test |
1. Pass the data | 3. Via this | Example | Test |
1. Pass the data | 4. Via tas | Example | Test |
2. Async tasks | 1. Async tasks | Example | Test |
2. Async tasks | 2. Mix tasks | Example | Test |
2. Async tasks | 3. The order of tasks | Example | Test |
2. Async tasks | 4. Fix callback hell (pyramid) | Example | Test |
3. As Promise | 1. Easier to use than Promise | Example | Test |
3. As Promise | 2. Use tas.all() as Promise.all() | Example | Test |
3. As Promise | 3. Use tas.race() as Promise.race() | Example | Test |
3. As Promise | 4. Cancel the unfinished tasks | Example | Test |
3. As Promise | 5. Use tas.forEach() | Example | Test |
4. Break the flow | 1. Ignore the current function | Example | Test |
4. Break the flow | 2. Break the current tasks | Example | Test |
4. Break the flow | 3. Abort Tas | Example | Test |
4. Break the flow | 4. Reset Tas | Example | Test |
5. Modularization | 1. Common module | A.js | Test |
5. Modularization | 2. Multiple modules | A.js, B.js | Test |
5. Modularization | 3. Dependent chain | A.js, B.js, C.js, D.js | Test |
6. Complex | 1. A crazy example | Example | Test |
For Web
Run the test of examples in your browser:
$ cd /path/to/tas/examples
$ open web/__tas/test.html
Or view the source code of the examples online:
Folder | Topic | Example | Test |
---|---|---|---|
1. Pass the data | 1. Hello world | Example | Test |
1. Pass the data | 2. Via return | Example | Test |
1. Pass the data | 3. Via this | Example | Test |
1. Pass the data | 4. Via tas | Example | Test |
2. Async tasks | 1. Async tasks | Example | Test |
2. Async tasks | 2. Mix tasks | Example | Test |
2. Async tasks | 3. The order of tasks | Example | Test |
2. Async tasks | 4. Fix callback hell (pyramid) | Example | Test |
3. As Promise | 1. Easier to use than Promise | Example | Test |
3. As Promise | 2. Use tas.all() as Promise.all() | Example | Test |
3. As Promise | 3. Use tas.race() as Promise.race() | Example | Test |
3. As Promise | 4. Cancel the unfinished tasks | Example | Test |
3. As Promise | 5. Use tas.forEach() | Example | Test |
4. Break the flow | 1. Ignore the current function | Example | Test |
4. Break the flow | 2. Break the current tasks | Example | Test |
4. Break the flow | 3. Abort Tas | Example | Test |
4. Break the flow | 4. Reset Tas | Example | Test |
5. Modularization | 1. Common module | A.js | Test |
5. Modularization | 2. Multiple modules | A.js, B.js | Test |
5. Modularization | 3. Dependent chain | A.js, B.js, C.js, D.js | Test |
6. Complex | 1. A crazy example | Example | Test |
For Web RequireJS
Run the test of examples in your browser:
$ cd /path/to/tas/examples
$ open web_requirejs/__tas/test.html
Or view the source code of the examples online:
Folder | Topic | Example | Test |
---|---|---|---|
1. Pass the data | 1. Hello world | Example | Test |
1. Pass the data | 2. Via return | Example | Test |
1. Pass the data | 3. Via this | Example | Test |
1. Pass the data | 4. Via tas | Example | Test |
2. Async tasks | 1. Async tasks | Example | Test |
2. Async tasks | 2. Mix tasks | Example | Test |
2. Async tasks | 3. The order of tasks | Example | Test |
2. Async tasks | 4. Fix callback hell (pyramid) | Example | Test |
3. As Promise | 1. Easier to use than Promise | Example | Test |
3. As Promise | 2. Use tas.all() as Promise.all() | Example | Test |
3. As Promise | 3. Use tas.race() as Promise.race() | Example | Test |
3. As Promise | 4. Cancel the unfinished tasks | Example | Test |
3. As Promise | 5. Use tas.forEach() | Example | Test |
4. Break the flow | 1. Ignore the current function | Example | Test |
4. Break the flow | 2. Break the current tasks | Example | Test |
4. Break the flow | 3. Abort Tas | Example | Test |
4. Break the flow | 4. Reset Tas | Example | Test |
5. Modularization | 1. Common module | A.js | Test |
5. Modularization | 2. Multiple modules | A.js, B.js | Test |
5. Modularization | 3. Dependent chain | A.js, B.js, C.js, D.js | Test |
6. Complex | 1. A crazy example | Example | Test |
APIs Examples
Tas provides a small amount of APIs to control the flow, and they are simple and easy to use, so you can focus on the code itself without wasting time on mastering Tas.
For Node.js
Pass The Data
API | Functions | Example | Test |
---|---|---|---|
return [1, 2, 3] | Pass 1, 2, 3 to the next function or tasks. | Example | Test |
this.foo = "bar" | The data is valid for the functions in the current task object. | Example | Test |
tas.foo = "bar" | The data is valid for the functions in all tasks and modules. | Example | Test |
Async Tasks
API | Functions | Example | Test |
---|---|---|---|
return "await" | Used in one of a group of sync tasks. | Example | Test |
tas.await() | If the tasks/subtasks contains async code, use it. | Example | Test |
tas.next() | Jump to the next function or tasks to continue. | Example | Test |
As Promise
API | Functions | Example | Test |
---|---|---|---|
tas.promise() | After this tasks is completed, continue. | Example | Test |
tas.all() | After all tasks are completed, continue. | Example | Test |
tas.race() | As long as one of tasks is completed, continue. | Example | Test |
tas.cancel() | Manually cancel the unfinished task(s). | Example | Test |
tas.forEach() | Perform a set of tasks for each array element. | Example | Test |
this.done | Pass the data received from promise to the next task. | Example | Test |
Break The Flow
API | Functions | Example | Test |
---|---|---|---|
return "ignore" | Ignore the current function. | Example | Test |
return "break" | Break the current tasks. | Example | Test |
return "abort" | Abort Tas. | Example | Test |
tas.break() | Break the current tasks from nested function (closures). | Example | Test |
tas.abort() | Abort Tas from nested function (closures). | Example | Test |
tas.reset() | Reset the status of Tas for running again. | Example | Test |
For Web
Pass The Data
API | Functions | Example | Test |
---|---|---|---|
return [1, 2, 3] | Pass 1, 2, 3 to the next function or tasks. | Example | Test |
this.foo = "bar" | The data is valid for the functions in the current task object. | Example | Test |
tas.foo = "bar" | The data is valid for the functions in all tasks and modules. | Example | Test |
Async Tasks
API | Functions | Example | Test |
---|---|---|---|
return "await" | Used in one of a group of sync tasks. | Example | Test |
tas.await() | If the tasks/subtasks contains async code, use it. | Example | Test |
tas.next() | Jump to the next function or tasks to continue. | Example | Test |
As Promise
API | Functions | Example | Test |
---|---|---|---|
tas.promise() | After this tasks is completed, continue. | Example | Test |
tas.all() | After all tasks are completed, continue. | Example | Test |
tas.race() | As long as one of tasks is completed, continue. | Example | Test |
tas.cancel() | Manually cancel the unfinished task(s). | Example | Test |
tas.forEach() | Perform a set of tasks for each array element. | Example | Test |
this.done | Pass the data received from promise to the next task. | Example | Test |
Break The Flow
API | Functions | Example | Test |
---|---|---|---|
return "ignore" | Ignore the current function. | Example | Test |
return "break" | Break the current tasks. | Example | Test |
return "abort" | Abort Tas. | Example | Test |
tas.break() | Break the current tasks from nested function (closures). | Example | Test |
tas.abort() | Abort Tas from nested function (closures). | Example | Test |
tas.reset() | Reset the status of Tas for running again. | Example | Test |
For Web RequireJS
Pass The Data
API | Functions | Example | Test |
---|---|---|---|
return [1, 2, 3] | Pass 1, 2, 3 to the next function or tasks. | Example | Test |
this.foo = "bar" | The data is valid for the functions in the current task object. | Example | Test |
tas.foo = "bar" | The data is valid for the functions in all tasks and modules. | Example | Test |
Async Tasks
API | Functions | Example | Test |
---|---|---|---|
return "await" | Used in one of a group of sync tasks. | Example | Test |
tas.await() | If the tasks/subtasks contains async code, use it. | Example | Test |
tas.next() | Jump to the next function or tasks to continue. | Example | Test |
As Promise
API | Functions | Example | Test |
---|---|---|---|
tas.promise() | After this tasks is completed, continue. | Example | Test |
tas.all() | After all tasks are completed, continue. | Example | Test |
tas.race() | As long as one of tasks is completed, continue. | Example | Test |
tas.cancel() | Manually cancel the unfinished task(s). | Example | Test |
tas.forEach() | Perform a set of tasks for each array element. | Example | Test |
this.done | Pass the data received from promise to the next task. | Example | Test |
Break The Flow
API | Functions | Example | Test |
---|---|---|---|
return "ignore" | Ignore the current function. | Example | Test |
return "break" | Break the current tasks. | Example | Test |
return "abort" | Abort Tas. | Example | Test |
tas.break() | Break the current tasks from nested function (closures). | Example | Test |
tas.abort() | Abort Tas from nested function (closures). | Example | Test |
tas.reset() | Reset the status of Tas for running again. | Example | Test |
Test
Clone the Tas repo (if you have not done so yet):
$ cd /path/to
$ git clone https://github.com/tasjs/tas.git
With Mocha
$ cd /path/to/tas
$ npm test
In Node.JS
Test with Node.js
$ cd /path/to/tas/test
$ node nodejs/test.js
In Web
Test in your browser:
$ cd /path/to/tas/test
$ open web/test.html
In Web RequireJS
Test in your browser:
$ cd /path/to/tas/test
$ open web_requirejs/test.html
Why is Tas faster than Promise?
Tas does not use setTimeout or similar methods recommended by the Promise standard. With no delay, Tas is faster than Promise and promise libraries such as bluebird, Q, aigle, when.js, etc.
References:
[1] "This can be implemented with either a 'macro-task' mechanism such as setTimeout or setImmediate, or with a 'micro-task' mechanism such as MutationObserver or process.nextTick." See details
[2] "Modern browsers impose a minimum delay of 4ms on every setTimeout, regardless of how long you specify. " See details
[3] "setImmediate() is designed to execute a script once the current poll phase completes." See details
[4] "All callbacks scheduled via process.nextTick() are run at the end of a phase of the event loop (e.g. timers) before transitioning to the next phase. " See details
License
Copyright (c) 2017, Owen Luke