It's been frequently used that we initiate a variable synchronously.
const x = call_a_sync_function();
But when the initiator becomes async, there would be a problem.
const x = await call_an_async_function(); // SyntaxError: Unexpected reserved word
I tried anonymous async initiator, it's not perfect.
let x;
(async () => x = await call_an_async_function())();
export function func() {
call(x); // x would be undefined if func() is called too early.
}
Then I tried to export the function in the anonymous async initiator, failed again.
(async () => {
const x = await call_an_async_function();
export function func() { // SyntaxError: Unexpected token export
call(x);
}
)();
So, is there a better solution?