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

06609d73ad2165c4aafcf65a1ddb9563?s=128

Greg Heo

May 21, 2018
Tweet

Transcript

  1. @gregheo C1 Mobile
 Summit

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

  3. None
  4. None
  5. None
  6. None
  7. None
  8. !

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

  10. networker.beginRequest(.read, success: { data in
 parser.parseRaw(data) { result in
 }

    })
  11. networker.beginRequest(.read, success: { data in
 parser.parseRaw(data) { result in
 archiver.materialize(from:

    result) { objects in
 } } })
  12. networker.beginRequest(.read, success: { data in
 parser.parseRaw(data) { result in
 archiver.materialize(from:

    result) { objects in
 dataStore.save(objects) { status in
 } } } })
  13. 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")
  14. ⏰#

  15. None
  16. A B C X Y Z

  17. 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")
  18. ⏰#

  19. 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!") } } } })
  20. Clojure

  21. Future

  22. (defn square [x]
 (* x x)
 )

  23. (defn square [x]
 (future (* x x))
 )

  24. (defn saveToDisk [data]
 (future (
 ...
 ))
 )

  25. saveToDisk future completed? cancelled? value

  26. @myFuture
 
 (deref myFuture)


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

  28. Future

  29. Promise

  30. @myPromise
 
 (deref myPromise)


  31. getValue promise realized? value promise deliver

  32. Promise

  33. %&