Ask Your Question
4

Is it possible to invoke an asynchronous function without using the await keyword?

asked 2021-12-05 11:00:00 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-04-12 14:00:00 +0000

lakamha gravatar image

Yes, it is possible to invoke an asynchronous function without using the await keyword.

One way to do so is by using the Promise.then() method. This method is called when the Promise is fulfilled (i.e., when the asynchronous function completes). Here is an example:

function myAsyncFunc() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve("Hello world");
    }, 1000);
  });
}

myAsyncFunc().then((result) => {
  console.log(result);
});

In this example, myAsyncFunc() returns a Promise that resolves to "Hello world" after a timeout of 1 second. We then call the Promise.then() method on the returned Promise, which logs the result to the console.

Note that using the await keyword is generally the recommended way to handle asynchronous functions, as it provides cleaner and more readable code.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-12-05 11:00:00 +0000

Seen: 18 times

Last updated: Apr 12 '21