author: Terence Zhong
email: texvnars@gmail.com
GitHub: https://github.com/TerenceZ/jsa.git
Description: This is a simple library for javascript asynchronous programming.
- supports automatical callback after satisfying a specific condition.At present, this lib supports the following conditions:
- when a timeout is met;
- when a function is accomplished;
- when an event is triggered;
- when an event handler is accomplished;
- when some parrallel sub-tasks are accomplished;
- supports chain operations, just like task.then(...).wait(...);
- supports async loop function;
- supports aborting a function;
- supports task manager;
- supports implicit task creation;
- supports arguments, result and exception propagation;
- so on;
- of course, you must include this lib;
- use "var task = [new] jsa.Task()" to create Task, or just use "task = jsa.Task.[then|once|wait|loop]" to create a implicit task;
- use "task.then(function(...) {...})" to add normal action;
- use "task.once(function(...) {...})" to add exception handler;
- use "task.loop(..., ...)" to add async loop action;
- use "task.wait(...[,...,...])" to add wait action;
- use "task.fire(...)" to start the task;
- use "task.abort(...)" to abort the task (including all sub-tasks);
- use "task.status" to check task running status;
- use "task.returnValue" to check return value;
- use "task.exceptInfo" to check exception info;
- use "task.reset()" to reset the task;
- use "jsa.taskManager.abort()" to abort all running tasks;
- use "jsa.taskManager" on console to check task manager status;
- You can easily to embrace some [parrallel] sub-tasks, just like this:
var task = jsa.Task.wait(500).then(function() {
jsa.Task.wait(500).then(function() {
console.log('a');
}).fire();
jsa.Task.then(function(s) {
jsa.Task.wait(300).then(function() {
console.log('b' + s);
}).fire();
}).fire(10);
}).then(function() {
console.log('c');
}).fire();
You can run it to check if the result is "b10 a c". If you want to abort this task, just use "task.abort()" or "jsa.taskManager.abort()" to abort all tasks.
- You can easily to listen the dom events and extend its handler, just like this:
var btn = document.createElement("input");
btn.type = "button";
btn.value = "Click me!";
document.body.appendChild(btn);
function go() {
jsa.Task.wait(500).then(function() {
console.log("hello");
}).fire();
}
var task = jsa.Task.wait(btn, "click", go).then(function() {
console.log("hi");
}).fire();
If you run it and click on the button, you can see the result is "hello hi"(delayed 500ms). If you click on the button again, you will just see the result is "hello"(delayed 500ms). If you use "task.abort()", you will just see "hello"(delayed 500ms), but if you use "jsa.taskManager.abort()" after button clicked but before the result shows, you will see nothing (otherwise you can still see "hello"). Why? Because the handler is decorated as a task.
- This lib is open-source, so you can modify it as what you want. But you should reserve the author messages of TerenceZ and this lib's copyright is reserved by TerenceZ.
- Hope you like it!