Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async/await in web browser or in node.js?

Tags:

Is there any attempt to bring async/await feature from C# 5.0 to any language which can be compiled to JavaScript (such as CoffeScript)? (So it can be used either in web browser or in node.js.)

like image 570
TN. Avatar asked Oct 09 '11 14:10

TN.


2 Answers

Async is on feature list for JavaScript harmony. So far there are numerous attempts to provide such functionality in the browser or in node, none of them seem to be compatible with harmony proposal though:

  • Async can be simulated with JS1.7 generators (see task.js). Not yet supported out-of-the-box by V8 (without experimental mode), but works in FF. Possibly traceur or Masacra compiler can be used to bring generators to other environments.
  • There is node-fibers library providing other mechanism for asynchronous programming in node (scarifies performance though). Other attempt basing on v8cgi is described here.
  • Rhino has continuations out of the box providing good alternative. This is why Ringo.js might be worth looking at.
  • Few solutions basing on js2js translation are available, e.g: jscx, NarrativeJS, jwacs, StratifiedJS. Some support integration with node.
  • There are many promise/future libraries trying to solve callbacks problem without extending syntax, however they all suffer from composability issues, i.e. cannot use language constructs like loops across callbacks.
like image 161
user212328 Avatar answered Sep 30 '22 04:09

user212328


async/await looks to be coming in ECMAScript 7. This proposal was accepted into stage 1 of the specification process in January 2014.

The good news is that Googles traceur compiler already supports it, so you could start using it today.

Sample syntax:

async function asyncValue(value) {   await timeout(50);   return value; } 

async/await is also on the TypeScript roadmap.

like image 41
Brett Postin Avatar answered Sep 30 '22 04:09

Brett Postin