We also can use an async/await statement with the asyncData method, for example:
// pages/using-async.vue
async asyncData (context) {
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Hello World by using async/await')
}, 2000)
})
const result = await promise
return { message: result }
}
In the preceding code, Nuxt will wait for 2 seconds for the promise to be resolved before rendering the page component with the 'Hello World by using async/await' message. Using the async/await statement is a new way of writing asynchronous JavaScript code. It is built on top of the Promise object and makes our asynchronous code more readable. We will use this statement often throughout the book.