task.js

Beautiful concurrency for JavaScript

  • 所有者: mozilla/task.js
  • 平台:
  • 许可证: Other
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

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] = yield join(read("foo.json"),
                                    read("bar.json")).timeout(1000);
        render(foo);
        render(bar);
    } catch (e) {
        console.log("read failed: " + e);
    }
});

Compared with callbacks:

var foo, bar;
var tid = setTimeout(function() { failure(new Error("timed out")) }, 1000);

var xhr1 = makeXHR("foo.json",
                   function(txt) { foo = txt; success() },
                   function(err) { failure() });
var xhr2 = makeXHR("bar.json",
                   function(txt) { bar = txt; success() },
                   function(e) { failure(e) });

function success() {
    if (typeof foo === "string" && typeof bar === "string") {
        cancelTimeout(tid);
        xhr1 = xhr2 = null;
        render(foo);
        render(bar);
    }
}
    
function failure(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.

主要指标

概览
名称与所有者mozilla/task.js
主编程语言JavaScript
编程语言JavaScript (语言数: 1)
平台
许可证Other
所有者活动
创建于2010-10-12 23:43:51
推送于2019-03-28 03:38:53
最后一次提交2013-11-27 23:37:13
发布数0
用户参与
星数1.6k
关注者数48
派生数73
提交数48
已启用问题?
问题数37
打开的问题数24
拉请求数2
打开的拉请求数6
关闭的拉请求数3
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?