Skip to content

Commit

Permalink
Merge pull request #24 from calebkleveter/draft
Browse files Browse the repository at this point in the history
Added Micro-tutorial and Example
  • Loading branch information
twof authored Feb 26, 2018
2 parents b2a56c5 + 563f572 commit 2cc576d
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions week4-18-02-19.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,19 @@ The community has been coming up with all sorts of useful extensions to core Vap
## Microtutorial
Standard promise chains are perfectly fine if only one `Future` needs to complete before you can move onto the next step, but what if you need the results from two `Future`s? One of the problems run into here is that you often need to start embedding promise callbacks and all of a sudden you're back to callback hell.

Let's say you have a route that creates a `User`. That user has some metadata that is connected to it, but you store it in a different database table to normalize your database. You need to create a `Pivot` so you can get the user's metadata. Because a pivot is based on the models ID's, both the models have to be saved in the database first. To make sure the models are saved before you create the pivot, you can use `Async`'s global `flatMap` method, which takes in a return type, and two or three futures. There is then a completion handler that passes in the results of the futures where you can create your pivot:

```swift
let user = // Create user here
let metadata = // Create metadata
flatMap(to: User.self, user.save(on: request), metadata.save(on: request)) { user, metadata in
return UserMetadata(user, metadata).save(on: request).transform(to: user)
}
```

***
## Credits:
[@twof](https://github.com/twof)
[@Cellane](https://github.com/Cellane)
[@bensyverson](https://github.com/bensyverson)
[@calebkleveter](https://github.com/calebkleveter)

0 comments on commit 2cc576d

Please # to comment.