From the course: Learning Koa

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Setup for async use

Setup for async use

From the course: Learning Koa

Start my 1-month free trial

Setup for async use

- [Instructor] So now that we understand what async is, let's look at code examples. And as mentioned before, you'll see it's very easy to use async in your code, so let me demonstrate. Let's get back to VS Code, and the first thing we're going to do is change some of the code that we have in our response. We don't need to change anything with our context user data but we're going to change something here. The first thing we'll need to do is add the async keyword inside of our function, app.use, here. And then what we're going to do is primarily the same thing that we've done here, so we're going to pass ctx inside of that function, and then use it inside of the async function now. And the async function works this way, because we're using asynchronous in our function, we need to try something so that's the syntax that typically goes with promises, so you'll do a try, and if within that try, or the function that we need to try here, doesn't work then we can catch it with an error, and…

Contents