6

For instance, why does the function below need to have "async"? Isn't using await specific enough for the compiler to parse the code without ambiguity?

// Why do we need async here?
async function foo() {
  var user = await getUser(user_id);
  console.log(user);
}

Is it for backward compatibility reasons? (I can't think of any code that uses the await keyboard in standard JavaScript...)?

Is it mainly for clarity to make it clear that this function uses the new async keyword?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
phzb0x
  • 69
  • 3
  • 1
    I can only imagine that it makes parsing easier.... – Felix Kling Feb 26 '16 at 16:10
  • 1
    In this example, it wouldn't make much sense. You're running something asynchronously and then telling it to wait for results from getUser. It might make more sense in the context of a larger function. – Neil Feb 26 '16 at 16:11
  • You might find this blog useful: https://www.twilio.com/blog/2015/10/asyncawait-the-hero-javascript-deserved.html I agree it's probably a parsing issue. From the blog: "When a function is declared as async it is then able to yield execution to the calling code while it awaits for a promise to be resolved. " – scrappedcola Feb 26 '16 at 16:14
  • 1
    I guess the why boils down to *"because the spec says so"*, or at least probably will when there is a spec, and that's the pattern commonly used in other languages implenting async/await. – adeneo Feb 26 '16 at 16:14
  • Much like function generators, why do they need a *? – Ruan Mendes Feb 26 '16 at 16:18
  • 3
    Fact is, in order to know for sure, you have to talk to the person you proposed this feature. This isn't really a good fit for Stack Overflow, even if it is an interesting question in itself. – Felix Kling Feb 26 '16 at 16:19
  • Why someone stupid always close others' questions even if they are good questions? – LCB May 05 '17 at 13:29
  • See also [Why is it necessary to have the async keyword before the function keyword](https://stackoverflow.com/q/31483342/1048572) – Bergi Jul 08 '17 at 14:14

1 Answers1

8

From a language perspective, the async/await keywords in JavaScript are designed very closely to the way they work in C#.

I have an old blog post that describes some discussions around why async was explicitly added in C#: see Inferring "async" here. In short, adding keywords is a potentially breaking change to a language; imagine an existing app that uses a var await = false; or something of that nature.

Or, for an example of how this could be more ambiguous, var await = function() {};, which would be used as await (x);. Looking at the usage await (x);, the compiler would have a hard time deciding what kind of expression that is. You could argue that await is a keyword unless there's a variable in scope with that name, but that gets really hairy.

A much cleaner solution is to introduce a pair of keywords, so async (which is used only for functions and lambdas, and is not ambiguous) enables the await keyword, but only within that scope. There are similar benefits to having function* denote generators, rather than just the presence of yield.

It's not only less ambiguous (maintaining backwards compatibility with code that uses await for other things), but it is also easier for both software and humans to parse.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810