Do check GitHub's page: https://github.com/ricmrodrigues/runasync
Usage:
Task:
Task.run(function([parameters]) { } [, Array parameters]) returns Promise
Dispatch execution to the UI thread (from within a Task.run or continueWith function callback):
dispatch(function() {})
Promise:
//'when' executes in the UI thread, immediately after the background thread finishes
when(function([parameters]) { }) return Promise
//'continueWith' spins up a new task with the callback provided
continueWith(function([parameters]) { }) returns Promise
Performance:
Using RunAsync: http://jsfiddle.net/v7m5p/12/
Running code synchronously: http://jsfiddle.net/UejYX/3/
Example:
for (var x=1;x<=3;x++) {
(function(idx) {
var task = Task.run(function(idx) {
var response = 0,
max = (89000000*(idx*3));
dispatch(function() {
//do some DOM manipulation that gets executed in the UI thread
});
for (var i=0;i<max;i++) {
i++;
if (i === max - 1) response = i;
}
return response;
}, [idx]);
task.when(function(value) {
console.log("executed from UI thread");
});
task.continueWith(function(value) {
//execute something else asynchronously
});
})(x);
}