If you need to use a Promise object in your module, for example, to fetch some async data from a remote API using an HTTP client, then Nuxt can perfectly support that. The following are some of the options you can write your async modules with.
Using async/await
You can use ES6 async/await in your module with Axios, the HTTP client that we have been using since Chapter 4, Adding Views, Routes, and Transitions, such as in the following example:
// modules/async-await/module.js
import axios from 'axios'
export default async function () {
let { data } = await axios.get(
'https://jsonplaceholder.typicode.com/posts')
let routes = data.map(post => '/posts/' + post.id)
console.log(routes)
}
// nuxt.config.js
modules: [
['~/modules/async-await/module']
]
In the preceding example, we use the get method from Axios to get all posts from the remote API, JSONPlaceholder (https://jsonplaceholder.typicode.com/). You should see the...