5 ms·
Using async.js (http://eligrey.com/blog/post/pausing-javascript-with-async-js http://eligrey.com/blog/post/pausing-javascript-with-async-j...), you can do away
by Sephr 16y ago
Using async.js (http://eligrey.com/blog/post/pausing-javascript-with-async-js http://eligrey.com/blog/post/pausing-javascript-with-async-j...), you can do away with callbacks and still have asynchronous code by using the yield statement. The following is an example (taken from my blog post) of asynchronous code using async.js:
yield to.request("/feedback", "POST", (
yield to.prompt("What are your impressions of async.js?")
));
yield to.inform("Thanks for your feedback!");
// do more stuff here
The equivalent code with callbacks is as follows:
async.prompt(
["What are your impressions of async.js?"],
function (response) {
async.request(
["feedback", "POST", response],
function () {
async.inform(
["Thanks for your feedback!"],
function () {
// do more stuff here
}
);
}
);
}
);
It makes it very easy to handle DOM events, as demonstrated below.
var clickEvent = yield myButton.next("click");
Full disclosure: I wrote async.js.
- Groxx 16y agoSince you wrote it: any idea what browser support of Javascript 1.7 is at? Wikipedia[1] currently thinks only Firefox has past 1.5, but I can't find anything else that isn't ancient (in web years), and Wikipedia's tables of things tend to get inaccurate pretty quickly. [1] http://en.wikipedia.org/wiki/JavaScript#Versions http://en.wikipedia.org/wiki/JavaScript#Versions
- Sephr 16y agoOnly Mozilla supports JavaScript (which is a mix of JavaScript-only features and ECMAScript; it's a little complicated), and everyone else implements ECMAScript. Some JavaScript features are in discussion of being added to ECMAScript, but no current ECMAScript engines (e.g. V8) have implemented them yet, barring the partial destructuring assignment implementation supported by Opera's Futhark ECMAScript engine.
- sid0 16y agoI think yield's coming to ECMAScript Harmony. That should simplify things a lot.