Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A Promise for a Better Future

A Promise for a Better Future

Synchronous code is easy understand. Inputs and outputs. It's easy to chain together. When you need to go asynchronous the model shifts. Typically you start using completion handlers or delegation, and then the code isn't quite as easy to reason about as it once was. You start passing around closures of different types, have error handling in multiple places, and the nesting becomes quite tedious. Worse, when you have to add things like retry, timeout, recovery, and concurrency things can get really tangled and be a source of bugs. In this talk we'll examine how Promises can turn your asynchronous code into something a little easier to work with, allowing you to compose asynchronous operations with ease and supporting advanced concepts in a clearer way. This talk will also examine a possible future of Swift where async/await is a reality.

Ben Scheirman

April 18, 2019
Tweet

More Decks by Ben Scheirman

Other Decks in Programming

Transcript

  1. Asynchronous Code func fetchUser(id: Int, completion: @escaping (User?, Error?) ->

    Void) fetchUser(id: 5) { user, error in guard error != nil else { print("ERROR: \(error)") return } guard let user = user else { return } // user! }
  2. Chaining Synchronous Code let user = fetchUser(id: 5) let avatar

    = downloadImage(user.avatar) imageView.image = avatar
  3. Chaining Asynchronous Code fetchUser(id: 5) { user, error in guard

    error != nil else { print("ERROR: \(error)") return } guard let user = user else { return } downloadImage(user.avatarURL) { image, error in if let image = image { DispatchQueue.main.async { self.imageView = image } } } }
  4. It gets more complex Int User Hometown Zip Coordinates Weather

    Authenticated with a token All I have is a User ID (i.e “5”) What I need is the weather in that user’s hometown….
  5. authenticate { token self.persistToken(token) self.fetchUser(id: 5) { user, error in

    guard let user = user else { self.handleError(error); return } let hometown = user.hometown self.fetchCoordinates(hometown.zip) { result in switch result { case let .failed(error): self.handleError(error) case let .success(lat, long): self.fetchWeather(CLLocationCoordinate2d(lat, long)) { weatherResult in switch weatherResult { case let .failed(error): self.handleError(error) case let .success(weather): self.displayWeather(weather) } } } } }
  6. authenticate { token self.persistToken(token) self.fetchUser(id: 5) { user, error in

    guard let user = user else { self.handleError(error); return } let hometown = user.hometown self.fetchCoordinates(hometown.zip) { result in switch result { case let .failed(error): self.handleError(error) case let .success(lat, long): self.fetchWeather(CLLocationCoordinate2d(lat, long)) { weatherResult in switch weatherResult { case let .failed(error): self.handleError(error) case let .success(weather): self.displayWeather(weather) } } } } }
  7. DispatchQueue.main.async? fetchUser(id: 5) .done { user in print("User: \(user.username)") }

    .catch { error in print("Uh-oh: \(error)") } Main Queue promise.done(on: .global()) { // on background queue }
  8. Always run this code… fetchUser(id: 5).done { user in print("User:

    \(user.username)") } .catch { error in print("Uh-oh: \(error)") } activityIndicator.startAnimating() .ensure { self.activityIndicator.stopAnimating() }
  9. .map { $0.hometown.zip } 
 .done { zip in print("Hometown

    zip: \(zip)") } .catch { error in print("Uh-oh: \(error)") } Transform Promise Values fetchUser(id: 5)
  10. .map { $0.hometown.zip }
 .done { zip in print("Hometown zip:

    \(zip)") } .catch { error in print("Uh-oh: \(error)") } Transform Promise Values fetchUser(id: 5) .map { $0.hometown.zip }
 Promise<User> Promise<String>
  11. Chaining Promises fetchUser(id: 5)
 .map { $0.hometown.zip }
 .then {

    fetchWeather(for: $0) } .done { weather in print("Weather: \(weather)”) } .catch { error in print("Uh-oh: \(error)") } Waits for new promise
  12. Chaining Promises fetchUser(id: 5)
 .map { $0.hometown.zip }
 .then {

    fetchWeather(for: $0) } .done { weather in print("Weather: \(weather)”) } .catch { error in print("Uh-oh: \(error)") } Promise<String> Promise<User> Promise<Weather>
  13. Collecting Values func heatWater() -> Promise<Water> func grindBeans() -> Promise<Grounds>

    func brew(water: Water, grounds: Grounds) -> Promise<☕>
  14. heatWater() .then { water in grindBeans().map { (water, $0) }

    } .then { water, grounds in 
 } Collecting Values
  15. heatWater() .then { water in grindBeans().map { (water, $0) }

    } .then { water, grounds in 
 brewCoffee(water: water, grounds: grounds) } Collecting Values
  16. Racing let timeout = after(seconds: 5) let promise1 = fetchExtraData()

    race(promise1.asVoid(), timeout) .done { _ in }
  17. Creating Promises func fetchUser(id: 5) -> Promise<User> { return Promise

    { seal in userDB.loaduserWithID(5) { user, error in } } }
  18. Creating Promises func fetchUser(id: 5) -> Promise<User> { return Promise

    { seal in userDB.loaduserWithID(5) { user, error in if let e = error { seal.reject(e) } } } }
  19. Creating Promises func fetchUser(id: 5) -> Promise<User> { return Promise

    { seal in userDB.loaduserWithID(5) { user, error in if let e = error { seal.reject(e) } else if let u = user { seal.fulfill(u) } } } }
  20. Retry func attempt<T>(maximumRetryCount: Int = 3, delayBeforeRetry: DispatchTimeInterval = .seconds(2),

    _ body: @escaping () -> Promise<T> ) -> Promise<T> { var attempts = 0 func attempt() -> Promise<T> { attempts += 1 return body() .recover { error -> Promise<T> in guard attempts < maximumRetryCount else { throw error } return after(delayBeforeRetry) .then(on: nil, attempt) } } return attempt() }
  21. Retry attempt(maximumRetryCount: 3) { uploadFile(path: pathToFile) } .then { remoteURL

    in //… } .catch { _ in // we attempted three times but still failed }
  22. PromiseKit ❤ URLSession firstly { URLSession.shared.dataTask(.promise, with: req) } .compactMap

    { result in guard let http = result.response as? HTTPURLResponse else { return nil } return (data: result.data, http: http) } .map { (result: (data: Data, http: HTTPURLResponse)) -> Joke in guard result.http.statusCode == 200 else { throw APIError.requestFailed(result.http, result.data) } return try JSONDecoder().decode(Joke.self, from: result.data) }
  23. “I admit I’m quite afraid of elevators, so I’m taking

    steps to avoid them” icanhazdadjoke.com
  24. A Future is a read-only container for a value that

    hasn’t arrived yet. A Promise is writable ( usually only once).
  25. Creating Futures (synchronously) func greet(_ req: Request) -> Future<String> {

    let eventLoop: EventLoop = req.eventLoop return eventLoop.newSucceededFuture(result: "Hello... McFly!") }
  26. Creating Futures (asynchronously) func greet(_ req: Request) -> Future<String> {

    let promise = req.eventLoop.newPromise(of: String.self) DispatchQueue.global().async { sleep(1) promise.succeed(result: "Great Scott!") } return promise.futureResult }
  27. Future.and let future1: Future<Todo?> = try Todo.find(1, on: req) let

    future2: Future<Todo?> = try Todo.find(2, on: req) future1.and(future2) .do { todo1, todo2 in print("Results: \(todo1) and \(todo2)") } PromiseKit: when(fulfilled:)
  28. Future.map func randomNumber(on worker: Worker) -> Future<Int> { return worker.future(Int.random(in:

    1...100)) } // Request is a Worker randomNumber(on: req) .map(to: String.self) { num in return "\(num) jiggawatts" } PromiseKit: map
  29. Future.flatMap Todo.find(1, on: req) .map(to: Todo.self) { maybeTodo in guard

    let todo = maybeTodo else { throw Abort(.notFound) } return todo } .flatMap { todo in return todo.delete(on: req) } PromiseKit: then
  30. func loadWebResource(_ path: String, completionBlock: (result: Resource) -> Void) {

    ... } func decodeImage(_ r1: Resource, _ r2: Resource, completionBlock: (result: Image) -> Void) func dewarpAndCleanupImage(_ i : Image, completionBlock: (result: Image) -> Void) func processImageData1(completionBlock: (result: Image) -> Void) { loadWebResource("dataprofile.txt") { dataResource in loadWebResource("imagedata.dat") { imageResource in decodeImage(dataResource, imageResource) { imageTmp in dewarpAndCleanupImage(imageTmp) { imageResult in completionBlock(imageResult) } } } } }
  31. func loadWebResource(_ path: String) async -> Resource func decodeImage(_ r1:

    Resource, _ r2: Resource) async -> Image func dewarpAndCleanupImage(_ i : Image) async -> Image func processImageData1() async -> Image { let dataResource = await loadWebResource("dataprofile.txt") let imageResource = await loadWebResource("imagedata.dat") let imageTmp = await decodeImage(dataResource, imageResource) let imageResult = await dewarpAndCleanupImage(imageTmp) return imageResult }
  32. func loadWebResource(_ path: String) async -> Resource func decodeImage(_ r1:

    Resource, _ r2: Resource) async -> Image func dewarpAndCleanupImage(_ i : Image) async -> Image func processImageData1() async -> Image { let dataResource = await loadWebResource("dataprofile.txt") let imageResource = await loadWebResource("imagedata.dat") let imageTmp = await decodeImage(dataResource, imageResource) let imageResult = await dewarpAndCleanupImage(imageTmp) return imageResult } func loadWebResource(_ path: String) async -> Resource