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
Promiseを実装してみよう
Search
Hiroki Kumamoto
November 22, 2018
Programming
0
270
Promiseを実装してみよう
Hiroki Kumamoto
November 22, 2018
Tweet
Share
More Decks by Hiroki Kumamoto
See All by Hiroki Kumamoto
How to implement timetable view on iOS
kumabook
0
86
bff_tips.pdf
kumabook
0
59
Tips of server-side rendering with react + redux
kumabook
0
67
プロトコルを使って複数サービスを跨いだmusic playerを実装する
kumabook
2
520
emacs helm likeな WebExtensions を作った話
kumabook
0
140
merges sort
kumabook
0
75
lightweight introducing with reactive cocoa
kumabook
0
53
basic_sort
kumabook
0
70
git_peco_tips
kumabook
0
26
Other Decks in Programming
See All in Programming
第5回日本眼科AI学会総会_AIコンテスト_3位解法
neilsaw
0
170
生成AIでGitHubソースコード取得して仕様書を作成
shukob
0
250
今からはじめるAndroidアプリ開発 2024 / DevFest 2024
star_zero
0
1k
testcontainers のススメ
sgash708
1
120
Zoneless Testing
rainerhahnekamp
0
120
開発者とQAの越境で自動テストが増える開発プロセスを実現する
92thunder
1
180
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
930
.NET 9アプリをCGIとして レンタルサーバーで動かす
mayuki
1
770
From Translations to Multi Dimension Entities
alexanderschranz
2
130
17年周年のWebアプリケーションにTanStack Queryを導入する / Implementing TanStack Query in a 17th Anniversary Web Application
saitolume
0
250
フロントエンドのディレクトリ構成どうしてる? Feature-Sliced Design 導入体験談
osakatechlab
8
4.1k
事業成長を爆速で進めてきたプロダクトエンジニアたちの成功談・失敗談
nealle
3
1.4k
Featured
See All Featured
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
510
It's Worth the Effort
3n
183
28k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Embracing the Ebb and Flow
colly
84
4.5k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
Six Lessons from altMBA
skipperchong
27
3.5k
KATA
mclloyd
29
14k
Thoughts on Productivity
jonyablonski
67
4.4k
Into the Great Unknown - MozCon
thekraken
33
1.5k
Agile that works and the tools we love
rasmusluckow
328
21k
Transcript
Promise Λ࣮ͯ͠ΈΑ͏ Hiroki Kumamoto a.k.a @kumabook We Are JavaScripters! @26th
͡Ίʹ • We Are JavaScripters! 2प͓ΊͰͱ͏͟͝ ͍·͢ʂ • About me:
• Twitter, GitHub: @kumabook • 3ճͷൃද
Promiseͱ • ඇಉظΛ͍͍ײ͡Ͱ͔͚Δͭ • MDNͷղઆ • ࡞࣌Ͱʢ݁Ռ͕)͔Βͳͯ͘Α͍ͷϓϩΩ γ • ޭࣦഊʹର͢ΔϋϯυϥʔΛؔ࿈͚Δ͜ͱ͕Ͱ͖
Δ • ಉظϝιουͱಉ͡Α͏ʹΛฦ͢͜ͱ͕Ͱ͖Δ
ྫ: BKBY \ VSMVSM TVDDFTT \BMFSU lTVDDFTTz ^
FSSPS \BMFSU lFSSPSz ^ ^ WBSQSPNJTFBKBY \VSMVSM^ QSPNJTFUIFO \BMFSU lTVDDFTTz ^ DBUDI \BMFSU lFSSPSz ^
• Promise.prototype.then(), Promise.prototype.catch()PromiseΛฦ͢ͷ ͰɺνΣʔϯʢ࿈ʣͰ͖Δ PromiseͷϝϦοτ BKBY \VSMVSM^ UIFO EBUB
BKBY \VSMVSM ʜ^ UIFO EBUB BKBY \VSMVSM ʜ^ DBUDI \BMFSU lFSSPSz ^
PromiseΛ࣮ͯ͠ΈΑ͏ • લ৬ͰΈࠐΈϒϥβ͚(es3)ʹ࣮ • es3ͳه๏Ͱ࣮ • catch͕༧ޠͳͷΛճආͳͲ • kumabook/yakusoku (174ߦ:
4KB) • jQuery.Deferred(Promiseͷલ)͕ಋೖ͞Εͨͷ͕ jQuery 1.5 (2011/1/31) • ࠓඪ४APIʹͳͬͯ͑ͯͨΓલ • ͳΜͱͳ͑ͯ͘ཧղͨ͠ؾʹͳ͍͕ͬͯͨࣗͰ࣮ͯ͠ษڧʹͳͬͨ
ࠓհ͢ΔAPIͷ࣮ • new Promise() • PromiseΦϒδΣΫτͷ࡞ • $.ajaxͳͲ͕෦తʹ͍ͬͯΔͷ • Promise.prototype.then()
• ޭɾࣦഊ࣌ͷϋϯυϥʔΛඥ͚ͮΔ
PromiseΦϒδΣΫτͷੜ • executor • resolveͱrejectΛҾʹͱΔ • resolve͔rejectͷͲͪΒ͔ҰํΛݺͼग़͢ • ޭͨ͠Βresolveɻࣦഊͨ͠Βreject •
͙͢ʹ࣮ߦ͞ΕΔ WBSQSPNJTFOFX1SPNJTF SFTPMWF SFKFDU \ TFU5JNFPVU \SFTPMWF GPP ^ ^
Promiseͷ࣮ߦঢ়ଶ • Promise ͷঢ়ଶ (pending, fulfilled, rejected) • fulfilled ͘͠rejectedʹͳͬͨঢ়ଶΛsettledͱ͍͏
QFOEJOH GVMpMMFE SFKFDUFE
Promiseͷঢ়ଶભҠ QFOEJOH GVMpMMFE SFKFDUFE 3VO FYFDVUPS SFTPMWF SFKFDU PS UISPXFSSPS
$BMM TVDDFTT IBOEMFS $BMM FSSPS IBOEMFS QSPNJTFͷঢ়ଶ QSPNJTFͷಈ࡞ FYFDVUPSͷಈ࡞
PromiseΛ࣮ͯ͠ΈΑ͏ • PromiseͷϓϩύςΟ • status: ঢ়ଶ(pending, fulfilled, rejected) • value:
ޭͨ࣌͠ͷ݁Ռɻࣦ͘͠ഊͨ͠ཧ ༝ • handlers: ޭͨ࣌͠ɺࣦഊͨ࣌͠ͷؔͷϖΞ ΛΩϡʔ(ྻ)Ͱอ࣋
PromiseͷίϯετϥΫλ • ϓϩύςΟͷॳظԽ • ҾͰ͖ͬͯͨexecutorΛ࣮ߦ
PromiseͷίϯετϥΫλ ϓϩύςΟͷॳظԽ WBS1SPNJTFGVODUJPO FYFDVUPS \ UIJTTUBUVT4UBUVT1&/%*/( UIJTWBMVFOVMM UIJTIBOEMFST<> UIJTSVO FYFDVUPS
^
ҾͰ͖ͬͯͨexecutorΛ࣮ߦ 1SPNJTFQSPUPUZQFSVOGVODUJPO FYFDVUPS \ WBSTFMGUIJT USZ\ FYFDVUPS GVODUJPO WBMVF \
TFMGGVMpMM WBMVF ^ GVODUJPO F \ TFMGSFKFDU F ^ ^DBUDI F \ TFMGSFKFDU F ^ ^ FYDFDVUPSࣗମ͕Τϥʔʹͳͬ ͨΒΩϟονͯ͠ΤϥʔΦϒδΣΫτͱ ͱʹϋϯυϥʔ FYDFDVUPS͕SFTPMWFͱ SFKFDUΛݺΜͰ͘ΕΔͷͰͦͷ݁Ռ Λϋϯυϥʔ
Promiseͷ࣮ߦঢ়ଶ QFOEJOH GVMpMMFE SFKFDUFE 3VO FYFDVUPS SFTPMWF SFKFDU PS UISPXFSSPS
$BMM TVDDFTT IBOEMFST $BMM FSSPS IBOEMFST TFMGGVMpMM TFMGSFKFDU
fulfillͱreject 1SPNJTFQSPUPUZQFGVMpMMGVODUJPO WBMVF \ WBSTFMGUIJT UIJTTUBUVT4UBUVT'6-'*--&% UIJTWBMVFWBMVF UIJTIBOEMFSTGPS&BDI GVODUJPO IBOEMFS
\ IBOEMFSPO'VMpMMFE TFMGWBMVF ^ UIJTIBOEMFST<> ^ ऴྃͨ͠QSPNJTFʹରͯ͠ UIFO͕ݺΕΔͷͰ
Promise.prototype.then() • ϋϯυϥͷϖΞΛొͭͭ͠ɺ৽͍͠PromiseΛฦ ͢ • ̎ͭϙΠϯτ͕͋Δ • ͢Ͱʹsettled(ྃࡁΈ)͔Ͳ͏͔ • Promise͕PromiseΦϒδΣΫτ(thenable)Ͱ
resolve͢Δ͔ʁ
Promise.prototype.then() • ͢Ͱʹsettled(ྃࡁΈ)͔Ͳ͏͔ • fulfilled or rejectedͳΒͦͷ··ϋϯυϥʔ ΛݺͿ • pendingͳΒexecutorͷ࣮ߦޙʹݺͿ
• queue(this.handlers)ʹੵΉ
Promiseͷ࣮ߦঢ়ଶ QFOEJOH 3VO FYFDVUPS SFTPMWF SFKFDU PS UISPXFSSPS ͜ͷؒʹUIFO ͜͜Ҏ߱ʹUIFO
Promise.prototype.then() • Promise͕PromiseΦϒδΣΫτ(thenable)Ͱresolve͢ Δ͔ʁ • ͠Promiseͷ݁Ռ͕PromiseΦϒδΣΫτͩͬͨ߹ ɺ͞ΒʹͦͷPromiseΦϒδΣΫτΛղܾͨ݁͠ՌΛ ࠾༻͢Δ • mapͱflatMap͕ϛοΫε͞Εͨڍಈ
• ಈతܕ͚ݴޠಛ༗ͳ༷
BKBY \VSMHFU6TFS6SM^ UIFO EBUB \ JG EBUBJT"ENJO \ SFUVSOBKBY
\VSMHFU%BUB6SM^ ^ SFUVSO<> ^ UIFO EBUB VQEBUF7JFX EBUB DBUDI \BMFSU lFSSPSz ^ QSPNJTFͱඇQSPNJTFͳ Λಁաతʹѻ͑Δ
1SPNJTFQSPUPUZQFUIFOGVODUJPO PO'VMpMMFE PO3FKFDUFE \ WBSTFMGUIJT SFUVSOOFX1SPNJTF GVODUJPO SFTPMWF SFKFDU \
TFMGFORVFVF0S&YFDVUF)BOEMFS GVODUJPO WBMVF \ JG WBMVFJOTUBODFPG1SPNJTF \ SFUVSOSFTPMWF WBMVFUIFO PO'VMpMMFE PO3FKFDUFE ^FMTF\ SFUVSOSFTPMWF PO'VMpMMFE WBMVF ^ ^ GVODUJPO F \ ʜ ^ ^ ^ ී௨ͷͷ߹ UIFOBCMFͷͷ߹
Promise API • ࠓճհͨ͠API • new Promise() • Promise.prototype.then() •
Γ • Promise.prototype.catch() • Promise.resolve(), Promise.reject() • Promise.all(), Promise.race()
·ͱΊ • PromiseΛ࣮Λհ • new Promise()ͱPromise.prototype.then() • ࣮γϯϓϧͱࢥ͍͖ͦͦ͜͜ෳࡶ • Stateful
+ asyncͳͷͰɺςετɾσόοάͣ͠Β͍ • Կؾͳ͍ͬͯ͘ΔAPIͷΛ༗ΓΈΛײ͡ΒΕ·͢
Thank you
ࢀߟࢿྉ • Promise - JavaScript | MDN • PromiseΛ͏ -
JavaScript | MDN • Promises/A+ • kumabook/yakusoku