Task.js Asynchronous Tasks In JavaScript
Written by Mike James   
Tuesday, 30 April 2013

Task.js is a Mozilla experiment in making asynchronous cooperative tasking in JavaScript look elegant. It works by re-purposing the new yield command in the latest version of JavaScript - this is very clever stuff!

It all seems to work but it needs ES6 and support for generators which means at the moment it only works in Firefox.

If you know a little JavaScript then you will also know that asynchronous programming is key to making it work correctly. You can't simply hog the single thread of execution of the result is that the system and the UI grinds to a halt. Yet for a language that depends on asynchronous programming JavaScript has little to offer in to help with what can be a very messy programming idiom.




The latest idea is to make use of generator functions and the new yield command. This allows a function to return a result to its caller much like a return. The difference is that the calling function can make the function resume from where it left off - its a limited form of a continuation.

The new framework implements a scheduler and uses the yield command to allow a function wrapped as a task to suspend its execution until some asynchronous action is completed. The main restriction is that the action called as part of the yield command has to return a Promise object. For example most jQuery asynchronous methods have at least an option to return a Promise or a Deferred object.


So what happens is the task that want to wait on an asynchronous call that returns a value simply uses:

var result = yield myAsynchMethod(arguments); 

The yield returns control to the scheduler along with the promise object returned by myAsynchMethod. The scheduler then either runs another task or returns its thread to process events for the UI. When myAsynchMethod is finished it resolves the promise and calls onResolve with the value that is its result. This wakes the scheduler up, again, and it takes the value and uses the send(value) command to resume the original task at the yield. This results in the value, and not the promise, being stored in result. 

Notice that as far as the programmer making use of the framework and the asynchronous method is concerned, there is no need to know anything about promises or how everything works. If you have an asynchronous method then you can call it in what appears to be a  synchronous way by simply calling it with yield.

As the task.js website puts it:

generators + promises = tasks

You can see a full example at the task.js website:

spawn(function*() {
 var data = yield $.ajax(url);
 var status = $('#status').html('Download complete.');
 yield status.fadeIn().promise();
 yield sleep(2000);


There are lots of fine details about how task.js works and it is a fascinating and very clever re-purposing of a feature added to ES6. After all, the real purpose of the yield is to make writing iterators easy and not to allow a asynchronous action to yield control to a calling function. 

Will task.js catch on? 

The first hurdle it has to negotiate is that ES6 has to be supported by all browsers and all, or at least the majority of, asynchronous operations need to return promises. Both of these conditions will need some time before they become true. 

Then there is the alternative approach to building a better asynchronous facility into JavaScript. Many programmers are of the opinion that C#'s async and await commands are the best way to do asynchronous operations. Perhaps a better solution than re-purposing yield would be to add a JavaScript async and await.

No matter what the future holds, task.js is well worth looking at if only to understand how it works. 



More Information


Related Articles

Task.js Asynchronous Tasks In JavaScript

jQuery, Promises & Deferred

jQuery Promises, Deferred & WebWorkers

Async, Await and the UI problem       

Why await? Why not multithread the UI?       


To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.






or email your comment to: comments@i-programmer.info



Coursera For New Year-Long Opportunity

Coursera, the online course provider which partners with leading universities and companies to deliver courses, certificates and even degrees has a great offer to welcome in 2021 - over $100 off a Cou [ ... ]

Genius Programmers Star In Gameshow

A two-episode series which debuted on Chinese streaming platforms last week has been described as the first reality competition to focus on programmers.

More News

Last Updated ( Monday, 06 May 2013 )