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
260
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
56
Tips of server-side rendering with react + redux
kumabook
0
65
プロトコルを使って複数サービスを跨いだmusic playerを実装する
kumabook
2
510
emacs helm likeな WebExtensions を作った話
kumabook
0
130
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
リリース8年目のサービスの1800個のERBファイルをViewComponentに移行した方法とその結果
katty0324
5
4.2k
ヤプリ新卒SREの オンボーディング
masaki12
0
110
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
4
600
イベント駆動で成長して委員会
happymana
1
270
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
1k
cXML という電子商取引の トランザクションを支える プロトコルと向きあっている話
phigasui
3
2.3k
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
170
Nurturing OpenJDK distribution: Eclipse Temurin Success History and plan
ivargrimstad
0
470
Better Code Design in PHP
afilina
PRO
0
110
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
640
開発効率向上のためのリファクタリングの一歩目の選択肢 ~コード分割~ / JJUG CCC 2024 Fall
ryounasso
0
420
RailsのPull requestsのレビューの時に私が考えていること
yahonda
5
2.8k
Featured
See All Featured
The Cult of Friendly URLs
andyhume
78
6k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Building Applications with DynamoDB
mza
90
6.1k
Practical Orchestrator
shlominoach
186
10k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
46
2.1k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
[RailsConf 2023] Rails as a piece of cake
palkan
51
4.9k
Bash Introduction
62gerente
608
210k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Speed Design
sergeychernyshev
24
600
It's Worth the Effort
3n
183
27k
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