Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async initiator in Javascript

Tags:

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?

like image 560
Jim Jin Avatar asked Oct 30 '18 05:10

Jim Jin


People also ask

What is async used for in JavaScript?

Async: It simply allows us to write promises based code as if it was synchronous and it checks that we are not breaking the execution thread. It operates asynchronously via the event-loop. Async functions will always return a value.

Why is async await used?

It allows a program to run a function without freezing the entire program. This is done using the Async/Await keyword. Async/Await makes it easier to write promises. The keyword 'async' before a function makes the function return a promise, always.

Is JavaScript async by default?

JavaScript is synchronous by default and is single threaded. This means that code cannot create new threads and it will execute your code block by order after hoisting.

Can we use await without async?

You can use the await keyword on its own (outside of an async function) within a JavaScript module. This means modules, with child modules that use await , wait for the child module to execute before they themselves run, all while not blocking other child modules from loading.


2 Answers

This is a special case of this problem. Asynchronous code can't be turned into synchronous. If a promise is used in an export then a promise should be exported:

const x = call_an_async_function();
export async function func() {
  call(await x);
}

Once there are promises, promise-based control flow is propagated everywhere the order of execution and error handling should be maintained, up to entry point:

import { func } from '...';

(async () => {
  await func();
  ...
})().catch(console.error);
like image 113
Estus Flask Avatar answered Sep 28 '22 07:09

Estus Flask


Wrap everything into an async function,

async function getX() => {let x = await call_an_async_function()); return x;}
function func() {
  call(x); // x would be undefined if func() is called too early.
}

async function main(){
   let x = await getX()
   func();
}

main()

Once top-level-await becomes part of ecmascript ( proposal), you wont have to wrap your await in async function and can use await at top level.

like image 41
Anurag Awasthi Avatar answered Sep 28 '22 05:09

Anurag Awasthi