TN. TN. - 1 month ago 8
Javascript Question

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

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.)

Answer

I'm not familiar with C#, but it sounds like what you're looking for is some sort of continuations, so that instead of writing

fs.readFile 'foo.txt', (err, data) ->
  myFunc data

you could instead just write something like

data = &fs.readFile 'foo.txt'  # not a real syntax
myFunc data

This isn't something that JavaScript or CoffeeScript provides. However, there are several other compilers that can do something like this:

  • TameJS - JavaScript-based, mainly just adds this feature
  • Kaffeine - JavaScript-based, adds a bunch of features
  • coco - CoffeeScript-based

See also: List of languages that compile to JavaScript on the CoffeeScript wiki.