Why is “asynchronous” not a reserved word?

As far as I can tell, both the spec and the documentation have await as the only reserved keyword from the async/await function.

This is further confirmed by the fact that we can name the async variable:

For instance:

 var async = 5; console.log(async) // this is fine 

Node (6.10) (also on Repl.it )

Node

Chrome (59)

Chrome

Firefox (54)

Firefox

Is it due to backward compatibility? I assume that many codebases will use the async name for certain functions.

This allows for some weird looking code examples:

 async function async() { var async = 5; await async; return async; } async().then(console.log) 

Endless recursive promise chain? (Not very important, as any function name allows this, however this code looks even more confusing)

 async function async() { await async(); } // stackoverflow (might need to open your console to see the output) 

+9
source share
1 answer

async need not be a reserved word, as it can be uniquely identified. The contexts in which it can occur are such as

 async function() { } async () => { } obj = { async foo() { } }; 

All of them could not be analyzed in any other way than to see async as an indication of the async function.

await , on the other hand, could theoretically be used in a type statement

 async function foo() { await(1); } 

which is ambiguous; await expects a value of 1 , or is it a function called with parameter 1 ? Therefore, await should be a reserved word (inside asynchronous functions, outside, feel free to use it as a variable).

Remember that JavaScript has changed significantly since its inception. Many words were designated as reserved, and then were never used or designated as reserved when, technically, they might not have needed it. The designation of await as a reserved word (inside modules) and not the designation of async as a reserved word are the product of a more mature understanding of the language by its designers.

+10
source

Source: https://habr.com/ru/post/1270244/


All Articles