This workshop will be retired on May 31, 2020.
Understanding the Async Module4:00 with Pasan Premaratne
Since Swift does not have any concurrency features built in, Vapor uses a custom async library to implement future based operations. In this video we take a quick tour on how to use the Async module.
With its latest rewrite, 0:00 Vapor 3 uses a non-blocking asynchrounous architecture by way of its Async module. 0:01 We're not going to get into the nitty-gritty of performance and 0:07 all that here, but 0:09 I do want to spend a quick second talking about how it affects the code we write. 0:10 In a non blocking architecture, 0:15 when a request is received by the server, it may not return a response immediately. 0:16 The server handles several requests at once, and 0:22 they're returned in the order completed. 0:24 When we send a request to the server, 0:27 it shuttles it off to the background until it's complete. 0:29 Because of this, we can't write syncronous methods 0:32 expecting a response to be returned immediately. 0:35 There are different ways to handle asynchronous operations, and 0:38 Vapor does it through the use of promises and futures. 0:41 If you're unfamiliar with the term, a future or 0:44 a promise, is considered to be a value that will eventually be available. 0:47 I'm not going to dive into futures and 0:52 promises here because asynchronous programming is a complicated topic. 0:53 On top of that, various languages and communities have developed their own 0:58 nuanced interpretations of what a promise or 1:02 future is that may conflict with other variations. 1:05 And we don't have the scope here to work through all of it. 1:08 That said, it is important to quickly understand how to work 1:11 with Vapor's implementation of futures. 1:14 In our code, let's say we send a request to the server asking for a single course. 1:16 Instead of having a function with a return type, 1:22 of course, the return type is a course wrapped in a future, like so. 1:24 What this means is that at some point when all async operations are complete, 1:30 that future is going to provide us with a course object. 1:34 Because a future is a value that will at some point be available, 1:38 when we con operation that returns a future, we can't write code that 1:42 acts on the underlying value since that's not available at the time. 1:46 Instead, we need to provide a closure that is executed when the future is resolved 1:50 and the underlying value actually becomes available. 1:55 There are three tools you have at your disposal when working with futures 1:58 in Vapor. 2:02 And that is flatMap(to:), map(to:), and transform(to:). 2:03 The choice of which of these tools you use depends on what return type you want. 2:08 So first, flatMap is used when you want to return another future. 2:14 Let's say that we send a request to a server to modify an instance of course in 2:19 the database. 2:23 First, you need to fetch that instance of course from the database. 2:24 Now this operation returns a future because the model will at some point 2:28 be available once the operation concludes, fetching from disk is not instantaneous. 2:32 Now from there, to modify the course and send it back in the response body, we 2:38 would call flatMap and provide a closure defining how we want to modify the object. 2:42 Since we're eventually sending back an instance of course wrapped in a future, 2:48 we call flatMap on the first operation because flatMap allows us to 2:54 execute some code and return a future. 2:58 Map is sort of the opposite and 3:01 is used when the closure provided returns a type other than a future. 3:03 Finally, we use transform when we want to ignore the result of a future and 3:08 return an entirely new future that has no relation to the first one. 3:12 A save operation to the database returns a future carrying the object saved. 3:16 We may not want to send this object back in the response, but 3:21 we still want to indicate that the operation succeeded. 3:24 We can use a transform here to ignore the resulting object of the save operation, 3:27 and instead send back a future carrying a status code. 3:31 Now, I understand that without much context and code, these concepts can be 3:35 hard to understand but just keep these three rules in mind as we move forward. 3:39 One, use flatMap when you want to return to future. 3:43 Two, use map when you don't want to return a future. 3:46 And three, use transform when you want to ignore the result of one future and 3:50 create an entirely new future to return. 3:54 Okay, in the next video let's put these concepts to use. 3:57
You need to sign up for Treehouse in order to download course files.Sign up