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

Promises, Futures, and the Shape of Your Code

Promises, Futures, and the Shape of Your Code

Greg Heo

May 21, 2018
Tweet

More Decks by Greg Heo

Other Decks in Technology

Transcript

  1. @gregheo
    C1 Mobile

    Summit

    View full-size slide

  2. ++++++++[>++++[>++>+
    ++>+++>+<<<<-]>+>+>-
    >>+[<]<-]>>.>---.+++
    ++++..+++.>>.<-.<.++
    +.------.--------.>>
    +.>++.

    View full-size slide

  3. networker.beginRequest(.read, success: { data in

    })

    View full-size slide

  4. networker.beginRequest(.read, success: { data in

    parser.parseRaw(data) { result in

    }
    })

    View full-size slide

  5. networker.beginRequest(.read, success: { data in

    parser.parseRaw(data) { result in

    archiver.materialize(from: result) { objects in

    }
    }
    })

    View full-size slide

  6. networker.beginRequest(.read, success: { data in

    parser.parseRaw(data) { result in

    archiver.materialize(from: result) { objects in

    dataStore.save(objects) { status in

    }
    }
    }
    })

    View full-size slide

  7. networker.beginRequest(.read, success: { data in

    parser.parseRaw(data) { result in

    archiver.materialize(from: result) { objects in

    dataStore.save(objects) { status in

    print("Save success!")
    }
    }
    }
    })
    print("Hello 1")

    print("Hello 2")

    View full-size slide

  8. networker.beginRequest(.read, success: { data in

    parser.parseRaw(data) { result in

    archiver.materialize(from: result) { objects in

    dataStore.save(objects) { status in

    print("Save success!")
    }
    }
    }
    })
    print("Hello 1")

    print("Hello 2")

    View full-size slide

  9. networker.beginRequest(.read, success: { data in

    parser.parseRaw(data) { result in

    archiver.materialize(from: result) { objects in

    dataStore.save(objects) { status in

    print("Save success!")
    }
    }
    }
    })

    View full-size slide

  10. (defn square [x]

    (* x x)

    )

    View full-size slide

  11. (defn square [x]

    (future (* x x))

    )

    View full-size slide

  12. (defn saveToDisk [data]

    (future (

    ...

    ))

    )

    View full-size slide

  13. saveToDisk
    future
    completed?
    cancelled?
    value

    View full-size slide

  14. @myFuture


    (deref myFuture)


    View full-size slide

  15. A
    X
    Y
    future
    B
    C
    D
    (deref future)

    View full-size slide

  16. @myPromise


    (deref myPromise)


    View full-size slide

  17. getValue
    promise
    realized?
    value
    promise
    deliver

    View full-size slide