Ch 4 Async - what do I need to know before reading?

Hi, I am thrilled to see this book and am learning right out of the gate. I don’t have much experience in this area, and Ch4 is a bit over my head. I was wondering if you could suggest some tutorials or video series that would get me up to speed? I don’t want to burn your time walking me through this on the forum, but if there was some “homework” I could do, please let me know.

For example, I need more detail here:

“To demonstrate, imagine you have a route that returns the HTTP status code 204 No Content. This route fetches a list of users from a database using a function like the one described above and modifies the first user in the list before returning.”

  • So, to explain this example, I have created some code like the example in Ch2 and have created a route that returns (aka responds to a GET operation) a list of users to the requesting machine? Is that correct?
  • “In order to use the result of that call…” - wait, I thought that I was writing the code that returned the list of users. Why am I unwrapping the result of this? Aren’t I just returning it? Am I confusing what “call” is?
  • Where can I find out more about the need to unwrap using map and flatMap? I haven’t seen these before in this context. Or maybe I am just forgetting…
  • Please explain more about these functions and why they are used in the situations mentioned. I am a bit lost here.

In #1 in the first mapping code, it says:
“The closure for flatMap(to:) receives the completed future - [User] as its parameter. This .flatMap(to:) returns Future.” - How do I know that it returns this type?

In #3, : “return user.save().map(to: HTTPStatus.self) { user in” - how does this work? I’ve never seen user.save().map(…) before

I basically have similar questions for the flatten section. Perhaps there could be more detail here or analogies used to describe what’s going on.

@afinque Thanks very much for your question!

I would recomment going through this tutorial to help become familiar with functional programming, and concepts like map, filter, reduce.

This should give you a good start. :slight_smile:

I hope this helps!

All the best!

1 Like

Awesome, thank you, I’ll check it out! You have answered a lot of my questions on the forums, and I appreciate all of the help. Thanks again!

Hey @afinque - to further answer your questions

You can see the return type by looking at the signature of flatMap(to:). flatMap actually takes two parameters, the return type of the closure, and the closure itself. In Swift, it’s typical to just call the closure after the function call without explicitly naming it. flatMap(to: T) will always return Future<T>. So if you do flatMap(to: User.self) { ... } that will return Future<User>

user.save() returns Future<User> - the saved user when the save has finished in the DB. Most of the time you want to actually interact with the saved user instead of the saved future user. map() essentially says “when this has returned, do this”

Hope that gets you started!

1 Like