Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using async await to get data loading

I try use async-await to get data on typescript vue 3, but this function already console when the data is undefined (or before function call working)

private async exportDataOrder() {
    await this.getDataExport()                          // function call
    let data = await this.controller.exportOrderData    // result function
    if (data) {
      console.log(data, 'dari vue')
      console.log('waiting')
    }
}
like image 500
Dhrmwnewn Avatar asked Apr 12 '21 07:04

Dhrmwnewn


People also ask

Why do we use async await with Fetch?

A better and cleaner way of handling the promise is through the async/await keywords. You start by specifying the caller function as async and then use await to handle the promise. Because of the await keyword, the asynchronous function pauses until the promise is resolved.

Is fetch API asynchronous?

forEach is synchronous, while fetch is asynchronous. While each element of the results array will be visited in order, forEach will return without the completion of fetch, thus leaving you empty-handed.

Does async await improve performance?

C# Language Async-Await Async/await will only improve performance if it allows the machine to do additional work.

What is async await used for?

The async and await keywords enable asynchronous, promise-based behavior to be written in a cleaner style, avoiding the need to explicitly configure promise chains. Async functions may also be defined as expressions.


1 Answers

Assuming that exportOrderData is an async function, you forgot the () to actually call your function.

let data = await this.controller.exportOrderData()
like image 141
Alex Wayne Avatar answered Nov 10 '22 01:11

Alex Wayne