Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
The Young Rappel
Search
Sergio Fernández
July 05, 2017
Programming
0
24
The Young Rappel
Futures and Promises on Swift
Sergio Fernández
July 05, 2017
Tweet
Share
More Decks by Sergio Fernández
See All by Sergio Fernández
Zero To Hero
fdzsergio
1
52
Other Decks in Programming
See All in Programming
ADRを一年運用してみた/adr_after_a_year
hanhan1978
7
2.4k
Node.js v22 で変わること
yosuke_furukawa
PRO
10
3.7k
try! Swift Tokyo 初参加報告LT
hinakko2
0
230
2 週間で Twitter Bot を作ってみた
contour_gara
0
710
Micro Frontends for Java Microservices - Devnexus 2024
mraible
PRO
0
510
大規模Reactアプリのリアーキテクチャ~8万行のTanStack Query移行の軌跡~
kj455
4
990
Deep Dive into React Stream/Serialize
mugi_uno
0
120
Behind VS Code Extensions for JavaScript / TypeScript Linnting and Formatting
unvalley
5
1.1k
CDKコントリビュートの最初の壁を越えよう! -簡単issueの見つけ方-
badmintoncryer
2
150
Introducing Kotlin Multiplatform in an existing mobile app - Workshop Edition | AndroidMakers Paris
prof18
0
130
0→1と1→10の狭間で Javaという技術選定を振り返る/Reflecting on the Decision to Choose Java Between Scaling from 0 to 1 and 1 to 10
jaguar_imo
2
390
Goのエラースタックトレースの歴史と今後
sonatard
9
1.7k
Featured
See All Featured
Happy Clients
brianwarren
92
6.4k
A better future with KSS
kneath
231
16k
Statistics for Hackers
jakevdp
790
220k
The Cost Of JavaScript in 2023
addyosmani
18
3.9k
The World Runs on Bad Software
bkeepers
PRO
61
6.7k
Building Better People: How to give real-time feedback that sticks.
wjessup
356
18k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
79
43k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
10
1k
Infographics Made Easy
chrislema
238
18k
Rebuilding a faster, lazier Slack
samanthasiow
74
8.2k
[RailsConf 2023] Rails as a piece of cake
palkan
26
4k
Bash Introduction
62gerente
605
210k
Transcript
The Young Rappel Futures & Promises
Hi! I'm Sergio @fdzsergio
Agenda Working with asynchronous tasks Deferred computations Back to the
Future
Async is hard
Request Image login(with: credentials) { (result: JSONResponse) in if case
.success(let json) = result, let user = User(with: json) { fetch(avatar: user.avatarURL) { (result: DataResponse) in if case .success(let data) = result { let image = UIImage(data: data) self.imageView = UIImageView(image: image) } } } }
then
Request Image login(with: credentials) .then(User.init) .then { user in return
fetch(avatar: user.avatarURL) }.then { data in self.imageView.image = UIImage(data: data) }.catch { error in // handle error } }
Futures & Promises It is the way to handle complex
asynchronous operations in a simple way
What the heck is a Future?
Future is an object that represents that you will get
something in the future.
Future let login: Promise<User> = login(with: credentials) login.then { user
in self.storage.save(user) }
What is a Promise?
A promise is an object that may produce a single
value some time in the future
Promise func login(with credentials: Credentials) -> Promise<User> { return Promise
{ fulfill, reject in self.client.request(task: .login(credentials)) { result in switch result { case .success(let json): fulfill(User(with: json)) case .failure(let error): reject(error) } } } }
Common Functions → then → catch → always → when
→ all
Limitations of Futures → Promises can only produce one value
→ Can nwot encapsulate streams of values → Only consumer driven
Back to the Future → Abstraction for async/sync operations →
Encapsulate a deferred computation → Centralized error handling → Easily composable
DeLorean
B-Sides → https://promisesaplus.com → http://khanlou.com/2016/08/promises-in-swift → https://speakerdeck.com/javisoto/back-to-the- futures → https://medium.com/compileswift/hydra-
promises-swift-c6319f6a6209
The Future is Today. — William Osler
Thank You!
Questions? @fdzsergio