How do I await on an async function returned by await?
Closed this issue · 4 comments
matAtWork commented
I've possibly (probably?) misunderstood either the precedence issues or contexts in which await is a keyword rather than a possible identifier, but clarification would be good.
This is from an issue at acornjs/acorn#309
// Return an async function asynchronously
async function x(n) {
return async function xxx(m) { return n+m }
}
async function test() {
var r = await x("a") ;
console.log(r) ; // [Function: xxx] - correct
console.log(await r("b")) ; // "ab" - correct
console.log(await (await x("c"))("d")) ; // [ReferenceError: await is not defined] - fail! await() looks like a call
console.log(await await x("e")("f")) ; // Fail! x("e")("f") binds to second await and fails (not a Promise)
}
test().then(console.log.bind(console),console.error.bind(console)) ;
sebmck commented
await
is a keyword inside of an async function and as a reserved word inside of module. The argument is parsed with the precedence of a UnaryExpression
.
matAtWork commented
Thx - so I can't reference any identifiers in an async function called 'await'? (Answering in the affirmative makes all my problems go away :) )....even if they are part of a closure, etc
bterlson commented
Correct. This is the same as generators except with yield
fwiw.
matAtWork commented
Much appreciated. Thanks