And the result is:
Let’s look at the output below:
Choosing a Global Software Development Partner to Accelerate Your Digital Strategy
To be successful and outpace the competition, you need a software development partner that excels in exactly the type of digital projects you are now faced with accelerating, and in the most cost effective and optimized way possible.
- Pending — Initial state of the program before the promise passes or fails
- Resolved — a successful promise
- Rejected — a failed promise
Let’s build a following code to clarify this:
If “hungry” is true in the code above, resolve the promise and return the data with a “fastfood” with the activity “Cook noodles,” otherwise return an error object with the message “Not hungry.”
When an async function returns a value, the promise is fulfilled with that value. however, if the async function throws an error, the promise is rejected along with that value.
In this declaration, we are defining the favoriteDrink() function, which returns the Monster energy drink. An async function that rejects a promise shows a rejected method that looks like this:
To guarantee that all promises produced by the async function are synchronised, await is included in the function. Call-backs are not used with async/await. To determine the rejection values of async functions, try and catch methods are also employed. Using the prior examples, let’s construct an async/await function enclosed in a try/catch method:
Here, we use async/await wrapped in try/catch approach
With ES2017+, you now make asynchronous queries to URL endpoints using the Fetch API and async/await. To do this, first declare the function as an async function, then wait for the response in json before returning your data. Let’s look at the code below to better understand this
The code above creates a fetch request to an external URL using the async method getJobAsync(). We then wait for the answer in json format and return the data after the request has been successfully completed.
Returning a Response from an Asynchronous Call