Library that allows you to execute JavaScript asynchronously seamlessly using modern browser capabitilies
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist
src
test
.travis.yml
Gruntfile.js
README.md
TODO.md
license
package.json

README.md

RunAsync v0.3.3 Build Status

Library that allows you to execute JavaScript asynchronously seamlessly using modern browser capabitilies with a syntax resembling .NET's Task Parallel Library.

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() {} [, Array parameters])

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/13/

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(max) {
                //do some DOM manipulation that gets executed in the UI thread
				
				//use max if we need to, this way we can send state from the background thread
				//to the UI thread
            }, [max]);		    
		    
		    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);
}