A beautiful way to control your task flow, simple and powerful. take a look at Demo and Automatic test
Think of the following code
setTimeout(function () {
// do something
setTimeout(function () {
// do something
setTimeout(function () {
// do something
setTimeout(function () {
// do something
setTimeout(function () {
// do something
},1000)
},1000)
},1000)
},1000)
},1000);
By this tiny script task.js
, you just need this
task()
.sleep(1000)
.run(function () {})
.sleep(1000)
.run(function () {})
.sleep(1000)
.run(function () {})
.sleep(1000)
.run(function () {})
.sleep(1000)
.run(function () {})
.start();
-
run(fn)
run function
params:
fn
- the function to be executed, the value fn returned will be the parammeter of the next fn. so it is easy to break a complex operation to mutiple simple tasks usingtask.js
returns:
the task object itself
-
sleep(time)
sleep some time
params:
time
- the waiting time to perform the next operation. time can be a function or number, if time is function, it use the returned value as the waiting time,and it will try to convert return value to number, it's parameter is the return value from last "run" function. so, it is easily to dynamiclly generate the timeout value according to the result of last opteration.returns:
the task object itself
-
start()
start execute task
params:
this function takes no params
returns:
this function has no return value
-
cancel()
stop execute task, the interrupt handler will be executed, see interrupt method
params:
this function takes no params
returns:
this function has no return value
-
progress(fn)
set the callback function to execute after each small task is executed
fn
- the callback function. this function will be passed three parameters.step
- the index of "run" function,totalStep
- the total step of the task,currentRepeat
- the index of repeat,totalRepeat
- total repeat times.returns:
the task object itself
-
done(fn)
set callback function when task is done, if the task is protected, that means there's no assertion errors. if task isn't protected, that means the task has finished.
params:
fn
- the callback function when task is donereturns:
the task object itself
-
interrupt(fn)
set callback function when task is interrupted. when the task is protected and there is assertion fail, the task will be interrupted.
params:
fn
- the callback function when task is done.detail
- fn will be fulfilled with error details.returns:
the task object itself
-
repeat(times)
set the times to repeat to do the task
params:
times
- the times to repeat to do the taskreturns:
the task object itself
-
assertEquals([fn,fn,...])
assert parammeters or it's return value is equal
params:
fn
- the function to be executed. it's parameter is the return value from last "run" function.returns:
the task object itself
-
assertNotEquals([fn,fn,...])
assert parammeters or it's return value isn't equal
params:
fn
- the function to be executed. it's parameter is the return value from last "run" function.returns:
the task object itself
-
assertFalse(fn)
assert parammeters or it's return value is false
params:
fn
- the function to be executed. it's parameter is the return value from last "run" function.returns:
the task object itself
-
assertTrue(fn)
assert parammeters or it's return value is true
params:
fn
- the function to be executed. it's parameter is the return value from last "run" function.returns:
the task object itself
-
protect()
protect the queue, once any assertion failed, the task is finished. this is the default behaviour.
params:
this function takes no params
returns:
this function returns nothing
-
unprotect()
unprotect the queue, ignore the assertion failed event, continue to execute the rest task.
params:
this function takes no params
returns:
this function returns nothing
onAssertionFail
Hook the assertion fail event
task.onAssertionFail = function (err) {
// err object contains error info
// or you can just throw it
throw err;
}
it follows the following routine
- if
onAssertionFail
is defined, just letonAssertionFail
handle it - if
window
hasconsole
object, and console has assert method, letwindow.console
handle it - if no handlers, just throw an
AssertionFail
error - stop the rest tasks
var i=1;
var result = 0;
task().run(function () {
result = result + i;
i = i + 1;
}).repeat(100).done(function () {
console.log(result); // output 5050
}).start();
var i=1;
var result = 0;
task().run(function () {
result = result + i;
i = i + 1;
}).sleep(500).run(function () {
console.log(result); // output result every 500ms
}).repeat(100).done(function () {
console.log(result); // output 5050
});
}).start();