Closed Bug 626756 Opened 14 years ago Closed 14 years ago

Add continuations (call/cc; feature request)

Categories

(Core :: JavaScript Engine, enhancement)

enhancement
Not set
normal

Tracking

()

RESOLVED WONTFIX

People

(Reporter: mielicki, Unassigned)

Details

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 Build Identifier: Continuations are a perfect match with JavaScript and event driven programming model. They could bring JS development to the whole new level. How about async(!) alert(fetch('http://example.com/data.txt'));? Continuations can be added with no new syntax thus without impact to standards conformance, please see Rhino as reference: http://wiki.apache.org/cocoon/RhinoWithContinuations Example of usage (tested on Rhino): // global scope continuation terminates execution var yield = new Continuation(); function fetch(url) { var xhr = new XMLHttpRequest(); var c = new Continuation(); // refers to the caller xhr.onreadystatechange = function() { if (xhr.readyState==4) { if (xhr.status==200) { c(xhr.responseText); // set return value upon continuation } else { c(null); } } } xhr.open("GET", url, true); xhr.send(null); yield(); // terminate execution } Reproducible: Always
What exactly makes the above "async"? fetch() has to produce a value before the alert runs, no?
Yes I does, but JS engine is free to run other callbacks while fetch is getting response over HTTP. Continuation resolves all the burden with async callbacks, another example: function hello() { console.log('Hello'); sleep(1000); // will continue when timer fires console.log('World'); } var yield = new Continuation(); function sleep(ms) { var c = new Continuation(); setTimeout(c, ms); yield(); } hello();
> Yes I does, but JS engine is free to run other callbacks Not without breaking current run-to-completion guarantees, right?
Right, but can't you cut yourself with a knife? There are no hazards introduced like with threads and it's a very powerful mechanism. Cooperative multithreading is widely used, and there already is a poor substitute in the form of yield statement.
Probably never going to happen.
Status: UNCONFIRMED → RESOLVED
Closed: 14 years ago
Resolution: --- → WONTFIX
Happened already in Rhino, also there are at least few attempts to provide continuation semantics by js2js translation, namely NarrativeJS and jwacs, and countless futile examples of resolving callbacks burden with some mimics of promises, function chaining and other hacks. Quite recently we where frightened with exceptions and it's now a standard language feature, and now we have yield in many. Cannot wait for continuations.
As one of the persons currently rewriting some of our JS code to make it async, I confirm that some form of continuation (call/cc would work, perhaps delimited continuations might be sufficient) would really, really, really make our life easier. Not to mention possibly clean up the code of |alert|, synchronous XHR, etc. So, here's my +1.
You need to log in before you can comment on or make changes to this bug.