In this example, the fetchData function is defined as an asynchronous function that returns a promise. The await keyword is used to pause the execution of the function until the promise is resolved, allowing the function to perform asynchronous operations without blocking the main thread.
When the fetchData function is called, it sends a request to a server using the fetch function and waits for the response. Once the response is received, the json method is used to parse the response and return it as JSON data.
The returned promise is then handled using the .then and .catch methods. If the promise is resolved successfully, the data is logged to the console. If an error occurs during the asynchronous operation, the error is caught and logged to the console.
Coded by Anshul Soni
https://anshulsoni.tech