Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

48 Commits

Repository files navigation

task.js

task.js provides an automatic task scheduler along with a library of first-class, synchronizable events, making it easy to do I/O without callbacks.

With task.js you can write non-blocking I/O in a synchronous style, even with error handling:

spawn(function*(){try{var[foo,bar]=yieldjoin(read("foo.json"),read("bar.json")).timeout(1000);render(foo);render(bar);}catch(e){console.log("read failed: "+e);}});

Compared with callbacks:

varfoo,bar;vartid=setTimeout(function(){failure(newError("timed out"))},1000);varxhr1=makeXHR("foo.json",function(txt){foo=txt;success()},function(err){failure()});varxhr2=makeXHR("bar.json",function(txt){bar=txt;success()},function(e){failure(e)});functionsuccess(){if(typeoffoo==="string"&&typeofbar==="string"){cancelTimeout(tid);xhr1=xhr2=null;render(foo);render(bar);}}functionfailure(e){xhr1&&xhr1.abort();xhr1=null;xhr2&&xhr2.abort();xhr2=null;console.log("read failed: "+e);}

...tasks can be a lot simpler and cleaner. And unlike pre-emptive threads, yield always makes it clear where tasks block.

Contributing

Currently the best way to contribute is to hang out on IRC: the channel is #task.js on irc.mozilla.org. Or you can always send me email (my Github nick at mozilla.com). And I'm always happy to accept pull requests!

If you're looking for interesting things to work on, check out the issue tracker.

About

Beautiful concurrency for JavaScript

Resources

Stars

1.6k stars

Watchers

46 watching

Forks

Releases

Packages

Used by

Contributors

Languages