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
310
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
100
bff_tips.pdf
kumabook
0
79
Tips of server-side rendering with react + redux
kumabook
0
78
プロトコルを使って複数サービスを跨いだmusic playerを実装する
kumabook
2
580
emacs helm likeな WebExtensions を作った話
kumabook
0
160
merges sort
kumabook
0
79
lightweight introducing with reactive cocoa
kumabook
0
55
basic_sort
kumabook
0
72
git_peco_tips
kumabook
0
30
Other Decks in Programming
See All in Programming
私の後悔をAWS DMSで解決した話
hiramax
4
140
LLMOpsのパフォーマンスを支える技術と現場で実践した改善
po3rin
8
980
🔨 小さなビルドシステムを作る
momeemt
2
530
自作OSでDOOMを動かしてみた
zakki0925224
1
1.4k
画像コンペでのベースラインモデルの育て方
tattaka
3
1.9k
AHC051解法紹介
eijirou
0
620
フロントエンドのmonorepo化と責務分離のリアーキテクト
kajitack
2
140
Nuances on Kubernetes - RubyConf Taiwan 2025
envek
0
190
ワープロって実は計算機で
pepepper
2
1.4k
コーディングは技術者(エンジニア)の嗜みでして / Learning the System Development Mindset from Rock Lady
mackey0225
2
580
The State of Fluid (2025)
s2b
0
200
学習を成果に繋げるための個人開発の考え方 〜 「学習のための個人開発」のすすめ / personal project for leaning
panda_program
1
110
Featured
See All Featured
How to train your dragon (web standard)
notwaldorf
96
6.2k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.6k
Facilitating Awesome Meetings
lara
55
6.5k
Making the Leap to Tech Lead
cromwellryan
134
9.5k
The Power of CSS Pseudo Elements
geoffreycrofte
77
5.9k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
161
15k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
The Art of Programming - Codeland 2020
erikaheidi
55
13k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.8k
Designing for humans not robots
tammielis
253
25k
Typedesign – Prime Four
hannesfritz
42
2.8k
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