// Encountering an error - 'await' is not recognized as a valid name.ts(2304)
let someVariable = await (async ():someType => {
// I require the use of await here, hence the need for async
return someValue;
})();
// The code below works but is not asynchronous.
let someVariable2 = (():someType => {
// Unable to use await in this context
return someValue
})();
I attempted enclosing await within another set of parentheses, but it still does not work. I am aware that I can declare a function and call it like usual, but I prefer this approach. If it's not feasible, I will revert back to the conventional method.
I'm uncertain about the functionality of ()
in situations like this, but I assume it returns the object inside. Is it viable to utilize async/await in this way? Additionally, I would appreciate learning more about the workings of ()
under these circumstances.
The code is executed on Deno.
edit: Some individuals are mentioning that "await must be used inside an async block." Deno supports top-level await.
To clarify:
// This is considered top-level
async function somefunc() {}
await somefunc(); // This WORKS in Deno.
The problem lies in
let var = await(async () => {})()
resulting in the aforementioned error. I am attempting to discover an alternative solution without having to explicitly declare and then await it