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
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Sergio Fernández
July 05, 2017
Programming
37
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
The Young Rappel
Futures and Promises on Swift
Sergio Fernández
July 05, 2017
More Decks by Sergio Fernández
See All by Sergio Fernández
Zero To Hero
fdzsergio
1
65
Other Decks in Programming
See All in Programming
フィードバックで育てるAI開発
kotaminato
1
120
Foundation Models frameworkで画像分析
ryodeveloper
1
120
【やさしく解説 設計編・中級 #4】ルールの寿命と、システムの年輪
panda728
PRO
2
150
【やさしく解説 設計編 #1】「ドメイン駆動」と「実装駆動」ってなに? 〜設計の考え方を、たとえ話で学ぼう〜
panda728
PRO
1
120
AI 輔助遺留系統現代化的經驗分享
jame2408
1
1.2k
1年で人数1.5倍、PR数5.5倍増。 品質とアウトカムはどうなったか、 何が効いたか
ike002jp
0
140
Generative UI & AI-Assistants for Your Angular Solutions
manfredsteyer
PRO
1
200
どこまでゆるくて許されるのか
tk3fftk
0
510
Prismを使った型安全な暗号化_関数型まつり2026
_fhhmm
0
140
AI時代の仕事技芸論〜ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ(スクフェス仙台 2026バージョン)
kuranuki
0
670
AI がコードを書く時代における新卒エンジニアの仕事風景 (2026) / New Graduate Engineers in the Era of AI Coding (2026)
sushichan044
0
220
使用 Meilisearch 建立新聞搜尋工具
johnroyer
0
160
Featured
See All Featured
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
400
Marketing to machines
jonoalderson
1
5.6k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
590
Embracing the Ebb and Flow
colly
88
5.1k
Navigating Weather and Climate Data
rabernat
0
390
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.7k
Visualization
eitanlees
152
17k
BBQ
matthewcrist
89
10k
Optimising Largest Contentful Paint
csswizardry
37
3.8k
How to make the Groovebox
asonas
2
2.3k
Become a Pro
speakerdeck
PRO
31
6k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
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