I would like to write taskjs coroutines, but I don't want to worry about
what functions might yield. Instead, I plan to allow any function to yield
by rewriting all my function calls to delegating yield:
Before:
function fetch_url(url) {
return yield create_promise_for_url(url); // will not work as
fetch_url is not yet a generator
}
function fetch_feed(name) {
return fetch_url(url_for_feed[name]);
}
function get_rss_feeds() {
return [
fetch_feed('bbc'), fetch_feed('slashdot'), fetch_feed('cnn')
];
}
var task = spawn(get_rss_feeds());
After:
function* fetch_url(url) {
return yield create_promise_for_url(url); // now this promise will be
passed all the way to spawn
}
function* fetch_feed(name) {
return yield* fetch_url(url_for_feed[name]);
}
function* get_rss_feeds() {
return [
yield* fetch_feed('bbc'), yield* fetch_feed('slashdot'), yield*
fetch_feed('cnn')
];
}
var task = spawn(get_rss_feeds());
I expect coroutine creation (the call to get_rss_feeds()) to be slow as it
needs to allocate a new stack. The nested yield* calls perform no stack
switching and could be as simple as normal function calls. The yield of the
promise would switch stacks, but is free to ignore all intermediary yield*
calls.
Essentially, I would like the Javascript VM to undo my rewrite. Do the
delegating yield semantics allow a VM to transform nested yield* calls into
normal function calls?
For reference, nested yield* calls in v8 are currently quite slow; compare
for example yield_fac(10) and yield_then_plain_fac(10) on
http://jsperf.com/delegating-yield.
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss