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
280
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
61
Tips of server-side rendering with react + redux
kumabook
0
67
プロトコルを使って複数サービスを跨いだmusic playerを実装する
kumabook
2
540
emacs helm likeな WebExtensions を作った話
kumabook
0
140
merges sort
kumabook
0
77
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
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
240
Amazon Nova Reelの可能性
hideg
0
190
PicoRubyと暮らす、シェアハウスハック
ryosk7
0
210
LLM Supervised Fine-tuningの理論と実践
datanalyticslabo
8
1.9k
Swiftコンパイラ超入門+async関数の仕組み
shiz
0
170
Package Traits
ikesyo
1
210
functionalなアプローチで動的要素を排除する
ryopeko
1
190
20241217 競争力強化とビジネス価値創出への挑戦:モノタロウのシステムモダナイズ、開発組織の進化と今後の展望
monotaro
PRO
0
280
ゼロからの、レトロゲームエンジンの作り方
tokujiros
3
1k
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
390
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
1.4k
非ブラウザランタイムとWeb標準 / Non-Browser Runtimes and Web Standards
petamoriken
0
430
Featured
See All Featured
4 Signs Your Business is Dying
shpigford
182
22k
Statistics for Hackers
jakevdp
797
220k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
Unsuck your backbone
ammeep
669
57k
Testing 201, or: Great Expectations
jmmastey
41
7.2k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Six Lessons from altMBA
skipperchong
27
3.6k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
49
2.2k
Thoughts on Productivity
jonyablonski
68
4.4k
The Power of CSS Pseudo Elements
geoffreycrofte
74
5.4k
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