1. Write an Asynchronous Function with async/await
Here, we have short function that talks with a github API.
|
|
It loads a specific user, and once the response comes back, it parses the body as JSON. Finally, user’s name and location are logged to the console.

Let’s see how we can convert this function into asynchronous function by using the async and await keywords.
- Make the function asynchronous by adding the
asynckeyword. - Using the
awaitoperator to wait until the fetch call complete.- The
awaitoperator takes a promise and pause the function execution until the promise is settled.
- The
- Assign the return value from
awaitoperator to a variable. - Take the body of the response and parse it as JSON.
- Print out the result.
|
|
To use this async/await, please user node 7.6 or up.
2. Call an Asynchronous Function in a Promise Chain
Now, showGithubUser function fetches the user from the github API, and then prints the name and location to the console. Let’s refactor this program such that the function only retrieves the user and returns to the caller who can decide what to do with it.
- Get rid of the
console.loglines and just return the user; theshowGithubUseris called, it returns the promise. - We can build a promise chain with the
.thenmethod.
|
|
This example shows that it is quite easy to call an asynchronous function as part of a promise chain. Every async function returns a promise, so you can simply call .then and .catch of the return value.
3. Convert Any Function into an Asynchronous Function
JavaScript allows us to convert any function into an async function. So far, we’ve worked function declaration, but we haven’t yet looked at function expressions, arrow functions, or methods. Let’s convert this function declaration into a function expression.
- Cut the name,
fetchGithubUser, and introduce a variable and then assign the function expression.
|
|
Let’s look at a good use case for an asynchronous function expression. The await keyword only be used in an asynchronous functions, so we cannot use it at the top level of the file.
|
|
Finally, let’s implement asynchronous class method.
- define a class with
GithubApiClientname. - define a method to fetch user data.
- put
asynckeyword in front of the method. - create an instance of the class
- call the method with using the instance
|
|
4. Handle Errors in Asynchronous functions
Let’s use the example from section 2. What happens if we tried to load a user that doesn’t actually exist? If you run the program you will get this.

Let’s see what the response object looks like. We will log out the user instead of individual property.

Our fetchGithubUser function always returns a promise which resolve this value. This is not what we want. Instead, we want to reject this promise with error message.
- Store the return value of the JSON to the variable
- If the status of the response is not successful, throw an error
- Add
catchmethod to our promise chain
|
|
This approach works because asynchronous function will automatically return a rejected promise whenever an error is thrown. This is what we can simply write a catch method and then deal with a rejected promise.
Another advantage of async/await keywords is that we can use regular try/catch statement. This is not possible plain promises because the callback function is always invoked asynchronously.
To illustrate how async/await is combined with try/catch blocks,
- convert promise chain into asynchronous function
- Add regular
try/catchstatement
|
|