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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Sergio Fernández
July 05, 2017
Programming
0
34
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
60
Other Decks in Programming
See All in Programming
AI時代の認知負荷との向き合い方
optfit
0
160
Honoを使ったリモートMCPサーバでAIツールとの連携を加速させる!
tosuri13
1
180
Fluid Templating in TYPO3 14
s2b
0
130
CSC307 Lecture 02
javiergs
PRO
1
780
AIによる開発の民主化を支える コンテキスト管理のこれまでとこれから
mulyu
3
260
Package Management Learnings from Homebrew
mikemcquaid
0
220
[KNOTS 2026登壇資料]AIで拡張‧交差する プロダクト開発のプロセス および携わるメンバーの役割
hisatake
0
280
そのAIレビュー、レビューしてますか? / Are you reviewing those AI reviews?
rkaga
6
4.6k
Lambda のコードストレージ容量に気をつけましょう
tattwan718
0
120
CSC307 Lecture 08
javiergs
PRO
0
670
LLM Observabilityによる 対話型音声AIアプリケーションの安定運用
gekko0114
2
430
コントリビューターによるDenoのすゝめ / Deno Recommendations by a Contributor
petamoriken
0
200
Featured
See All Featured
AI Search: Where Are We & What Can We Do About It?
aleyda
0
6.9k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
57
50k
Darren the Foodie - Storyboard
khoart
PRO
2
2.4k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.4k
30 Presentation Tips
portentint
PRO
1
210
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
84
Exploring anti-patterns in Rails
aemeredith
2
250
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
430
Believing is Seeing
oripsolob
1
54
エンジニアに許された特別な時間の終わり
watany
106
230k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
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