Exploring async methods and data fetching
Asynchronous functions in JavaScript are defined by the async
syntax and return a Promise. These functions operate asynchronously via the Event loop, using an implicit promise, which is an object that may return a result in the future.
As part of the JavaScript language, you can declare asynchronous blocks of code inside a Vue component’s method by including the async
keyword in front of a method.
You can use Promise chaining methods, such as the then()
and catch()
functions or try the await
syntax of ES6 inside these Vue methods and return the results accordingly.
Here is an example using the built-in fetch
API to fetch data inside a component method as an asynchronous function with async/await
keywords:
export default { Â Â methods: { Â Â Â Â async getAdvice() { Â Â Â Â Â Â const response = Â Â Â Â Â Â Â Â await fetch('https://api.adviceslip...