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
JavaScriptのPromiseをわかりやすく説明したい
Search
chihaso
February 15, 2020
Programming
0
770
JavaScriptのPromiseをわかりやすく説明したい
勉強していてなかなか理解できずつらかったPromiseについて、自分なりにわかりやすい形で、実例を出して説明します。
chihaso
February 15, 2020
Tweet
Share
More Decks by chihaso
See All by chihaso
孤独のRubyKaigi
chihaso
0
85
CSVこわい - 複雑なフォーマットのCSVを取り込むアプリを作りました
chihaso
1
68
Other Decks in Programming
See All in Programming
Click-free releases & the making of a CLI app
oheyadam
2
100
Googleのテストサイズを活用したテスト環境の構築
toms74209200
0
300
RailsのPull requestsのレビューの時に私が考えていること
yahonda
5
2.7k
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
440
JaSST 24 九州:ワークショップ(は除く)実践!マインドマップを活用したソフトウェアテスト+活用事例
satohiroyuki
0
320
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
4
1.9k
イベント駆動で成長して委員会
happymana
1
260
Realtime API 入門
riofujimon
0
140
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
270
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.2k
Importmapを使ったJavaScriptの 読み込みとブラウザアドオンの影響
swamp09
4
1.3k
CSC509 Lecture 09
javiergs
PRO
0
140
Featured
See All Featured
How to Ace a Technical Interview
jacobian
276
23k
A Philosophy of Restraint
colly
203
16k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Intergalactic Javascript Robots from Outer Space
tanoku
268
27k
The Invisible Side of Design
smashingmag
297
50k
Designing for humans not robots
tammielis
249
25k
Statistics for Hackers
jakevdp
796
220k
How GitHub (no longer) Works
holman
310
140k
Typedesign – Prime Four
hannesfritz
40
2.4k
Measuring & Analyzing Core Web Vitals
bluesmoon
2
75
For a Future-Friendly Web
brad_frost
175
9.4k
Become a Pro
speakerdeck
PRO
25
5k
Transcript
+BWB4DSJQUͷ1SPNJTFΛ Θ͔Γ͘͢આ໌͍ͨ͠ ઍ༿༤ଠɹ!DIJIBTP ʢʣ
ࣗݾհ wDIJIBTPʢͪͦʣͰ͢ wϑΟϤϧυϒʔτΩϟϯϓͰษڧத ਐߦ w͖ͳͷɿΞχϝɺອըɺΓɺྉཧɺԻָɺ ञʂʂɺϓϩάϥϛϯάʂɹ w ྛాٿɺߴڮ༿հɺϓϩϝΞɺUIFQJMMPXTɺ3):.&45&3FUD
ڭՊॻΛશ෦ಡΜ͕ͩɺ+BWB4DSJQU ͷඇಉظϓϩάϥϛϯά͕Θ͔Βͳ ͗ͯͭ͢Β͔ͬͨ ಛʹϓϩϛε͕ͭΒ͍ ͜ͷςʔϚΛબΜͩഎܠ
ࣗͳΓʹΘ͔Γ͘͢ આ໌ͯ͠Έ͍ͨ ωλόϨɿؤு͚ͬͨͲɺ͋Μ·ΓΘ͔Γ͘͢ͳ͍Ͱ͢
+BWB4DSJQUͰͷඇಉظϓϩάϥϛϯάͷํ๏Λɺ ςΩετϑΝΠϧΛಡΈࠐΉϓϩάϥϜΛྫʹͯ͠આ໌͠·͢ɻ ͜Ε͔Β͢͜ͱ ɾඇಉظϓϩάϥϛϯάͱʁ ɾίʔϧόοΫʹΑΔ࣮ݱํ๏ ɾϓϩϛεʹΑΔ࣮ݱํ๏ ɾൺֱ BUYU a.txtͷ༰ͩΑ CUYU
b.txtͷ༰ͩΑ DUYU c.txtͷ༰ͩΑ a.txtͷ༰ͩΑ b.txtͷ༰ͩΑ c.txtͷ༰ͩΑ ࣮ߦ݁Ռ +BWB4DSJQUͷ ϓϩάϥϜ ̏ͭͷςΩετϑΝΠϧΛಡΈࠐΜͰɺͦͷॱ൪ͷ··ίϯιʔϧ্ʹදࣔ ˞্هΛ࣮ݱ͢ΔʹαʔόʔΛ ཱͯΔඞཁ͕͋Γ·͕͢ɺͦ͜ ׂѪ͠·͢
ඇಉظϓϩάϥϛϯάͬͯʁ ϢʔβʔͷϘλϯΫϦοΫɺσʔλͷϩʔυͷΑ͏ͳ ඇಉظΠϕϯτ͕ൃੜͨ͠ࡍͷॲཧΛهड़͢Δ͜ͱ
+BWB4DSJQUͰඇಉظϓϩάϥϛϯά͢ΔͨΊͷಓ۩ ίʔϧόοΫ ϓϩϛε ଞʹ͋ΔΒ͍͚͠ͲͱΓ͋͑ͣɾɾɾ
ϑΝΠϧͷಡΈࠐΈɿ͜͏ॻ͍ͯμϝ const prefix = "http://localhost:7070/"; const xhr1 = new XMLHttpRequest();
xhr1.open("GET", prefix + "a.txt"); xhr1.send(); console.log(xhr1.response); console.log("͜Ε͕࠷ऴߦͰ͢"); ࣮ߦ݁Ռ ϩʔυΛͨͣʹ࣮ߦ͞ΕΔ DBMMCBDLKT BUYUΛ(&5͢ΔϦΫΤετͩΑ YIS͍ͯ͏໊લͷ9)3ΦϒδΣΫτ࡞ͬͯͶ ϦΫΤετඈͯ͠Ͷ ϨεϙϯεΛίϯιʔϧʹදࣔͯ͠Ͷ
ϑΝΠϧͷಡΈࠐΈɿίʔϧόοΫΛ͏ ؔʹ͞ΕͨؔΛίʔϧόοΫؔͱ͍͏ const prefix = "http://localhost:7070/"; const xhr1 = new
XMLHttpRequest(); xhr1.open("GET", prefix + "a.txt"); xhr1.addEventListener("load", event => console.log(xhr1.response)); xhr1.send(); console.log("͜Ε͕࠷ऴߦͰ͢"); ࣮ߦ݁Ռ ϩʔυͰ͖ͨΒϨεϙϯεΛίϯιʔϧʹදࣔͯ͠Ͷ
ϑΝΠϧͷಡΈࠐΈɿίʔϧόοΫࠈ ඇಉظॲཧΛͭͳ͛Α͏ͱ͢Δͱωετ͞Εͯݟ௨͕͠ѱ͍ const prefix = "http://localhost:7070/"; const xhr1 = new
XMLHttpRequest(); xhr1.open("GET", prefix + "a.txt"); xhr1.addEventListener("load", event => { const xhr2 = new XMLHttpRequest(); xhr2.open("GET", prefix + "b.txt"); xhr2.addEventListener("load", event => { const xhr3 = new XMLHttpRequest(); xhr3.open("GET", prefix + "c.txt"); xhr3.addEventListener("load", event => { console.log(xhr3.response); }); xhr3.send(); console.log(xhr2.response); }); xhr2.send(); console.log(xhr1.response); }); xhr1.send(); console.log("͜Ε͕࠷ऴߦͰ͢"); DBMMCBDL@IFMMKT ࣮ߦ݁Ռ
ڞ௨෦Λ·ͱΊͯ·ͩͭΒ͍ const prefix = “http://localhost:7070/"; function openFile(url, onload) { const
xhr = new XMLHttpRequest(); xhr.open("GET", url); xhr.addEventListener("load", e => { onload(e, xhr); console.log(xhr.response); }); xhr.send(); } openFile(prefix + "a.txt", (event, xhr) => { openFile(prefix + "b.txt", (event, xhr) => { openFile(prefix + "c.txt", (event, xhr) => {}); }); }); console.log("͜Ε͕࠷ऴߦͰ͢"); DBMMCBDL@IFMMKT ࣮ߦ݁Ռ ϑΝΠϧͷಡΈࠐΈɿίʔϧόοΫࠈ
ɾ+BWB4DSJQUʹ1SPNJTFͱ͍͏Ϋϥε͕͋Δ ɾ1SPNJTFͷΠϯελϯεඇಉظॲཧΛϥοϓͯ͠͏ ɾ1SPNJTF̏ͭͷঢ়ଶΛऔΔ ɹɹɾGVMpMMFEʢྃʣɹॲཧ͕શͯޭͨ͠ ɹɹɾSFKFDUFEʢغ٫ʣɿ్தͰΤϥʔ͕ൃੜͨ͠ ɹɹɾQFOEJOHɿྃغ٫͞Ε͍ͯͳ͍ ɾUIFOϝιουͰ1SPNJTF͕ྃͨ͠Β࣮ߦ͢ΔॲཧΛهड़͢Δ ϓϩϛεͱʁ
ϓϩϛεͷ͍ํΠϝʔδ 1SPNJTF ඇಉظॲཧ UIFO IPHF ʜ GVODUJPOIPHF ʜ \
DPOTUQOFX1SPNJTF ʜ \ ɾ ɾ ɾ ^ SFUVSOQ ^ 1SPNJTFΠϯελϯεΛฦؔ͢IPHF IPHF ʜ 1SPNJTF ඇಉظॲཧ UIFO IPHF ʜ 1SPNJTF ඇಉظॲཧ UIFO IPHF ʜ ɾ ɾ ɾ
ϑΝΠϧͷಡΈࠐΈɿϓϩϛεΛ͏ function openFile(url) { const p = new Promise(function(onFulfilled, onRejected)
{ const xhr = new XMLHttpRequest(); xhr.open("GET", url); xhr.addEventListener("load", e => { console.log(xhr.responseText); onFulfilled(); }); xhr.send(); }); return p; } const prefix = "http://localhost:7070/"; openFile(prefix + "a.txt") .then(() => openFile(prefix + "b.txt")) .then(() => openFile(prefix + "c.txt")); console.log("͜Ε͕࠷ऴߦͰ͢"); QSPNJTF@QBSBEJTFKT ࣮ߦ݁Ռ
ϓϩϛεΛੜͯ͠Δ෦Λॻ͖Լ͢ const p = new Promise( function(onFulfilled, onRejected) { const
xhr = new XMLHttpRequest(); xhr.open("GET", url); xhr.addEventListener("load", e => { console.log(xhr.responseText); onFulfilled(); }); xhr.send(); } ); 1SPNJTFΠϯελϯεΛ࡞ΔΑ ɹ1SPNJTFͷίϯετϥΫλʹԾҾPO'VMpMMFE ͱPO3FKFDUFEͯ͠ ɹɹఆYISͱ͍͏9)3ΦϒδΣΫτΛͭͬͯ͘Ͷ ɹɹYISVSMͷϦιʔεΛ(&5͢ΔϦΫΤετͩΑ ɹɹϦΫΤετඈͯ͠σʔλ͕ϩʔυ͞ΕͨΒҎԼΛ࣮ߦͯ͠Ͷ ɹɹ ɹɹɹɹϨεϙϯεͷςΩετΛίϯιʔϧʹදࣔͯ͠Ͷ ɹɹɹɹԾҾPO'VMpMMFEΛ࣮ߦͯ͠Ͷ ɹɹɹɹʢPO'VMpMMFEؔʣ ɹɹ ɹɹϦΫΤετඈͯ͠Ͷ
ϓϩϛεͷத͜Μͳײ͡ʢͨͿΜʣ 1SPNJTFΦϒδΣΫτΛ࡞Δ 1SPNJTFΦϒδΣΫτͷԾҾPO'VMpMMFE ͱPO3FKFDUFE ɹɹYISͱ͍͏ϦΫΤετΦϒδΣΫτΛ࡞Δ ɹɹ63-ͷϦιʔεΛ(&5͢ΔϦΫΤετ ɹɹϦΫΤετඈͯ͠σʔλ͕ϩʔυ͞ΕͨΒҎԼΛͬͯͶ ɹɹʢ͙͢Εͱݴͬͯͳ͍ʣ ɹɹɹɹϨεϙϯεͷςΩετΛίϯιʔϧʹදࣔ͢Δ ɹɹɹɹԾҾPO'VMpMMFEʹϦΫΤετΦϒδΣΫτ࣮ͯ͠ߦ͢Δ
ɹɹɹɹʢڻዼͷਅ࣮ɿPO'VMpMMFEؔʣ 1SPNJTFΦϒδΣΫτ ໊લͷͳ͍ؔ ୈҾʢؔʣ ʢ͜Εઈରඞཁʣ ୈҾʢؔʣ ඇಉظతʹ࣮ߦ͍ͨ͜͠ͱ ɾ ɾ ɾ const p = new Promise( function(onFulfilled, onRejected) { const xhr = new XMLHttpRequest(); xhr.open("GET", url); xhr.addEventListener("load", e => { console.log(xhr.responseText); onFulfilled(); }); xhr.send(); } ); ྃ࣌ʹ࣮ߦ͍ͨ͜͠ͱ ɾ ɾ ɾ غ٫࣌ʹ࣮ߦ͍ͨ͜͠ͱ ɾ ɾ ɾ
ͷײɿ ɹਖ਼ɺίʔϧόοΫ͚ͩͷํ͕Θ͔Γ͍͢ͷͰɾɾɾʁ
UIFOνΣʔϯΛGPSͰ·ͱΊΒΕΔ function openFile(url) { const p = new Promise(function(onFulfilled, onRejected)
{ const xhr = new XMLHttpRequest(); xhr.open("GET", url); xhr.addEventListener("load", e => { console.log(xhr.responseText); onFulfilled(); }); xhr.send(); }); return p; } const prefix = “http://localhost:7070/"; const dataIds = ["a", "b", "c"]; let promise = Promise.resolve(); // ղܾࡁΈͷۭͷPromiseΛ࡞Δ for (const id of dataIds) { promise = promise.then(() => openFile(prefix + `/${id}.txt`)); } console.log("͜Ε͕࠷ऴߦͰ͢"); QSPNJTF@QBSBEJTFKT ࣮ߦ݁Ռ
ൺֱϑΝΠϧݸ const dataIds = ["a", "b", "c"]; let promise =
Promise.resolve(); for (const id of dataIds) { promise = promise.then(() => openFile(prefix + `${id}.txt`)); } ίʔϧόοΫ openFile(prefix + "a.txt", (event, xhr) => { openFile(prefix + "b.txt", (event, xhr) => { openFile(prefix + "c.txt", (event, xhr) => {}); }); }); ϓϩϛε
ൺֱϑΝΠϧݸ const dataIds = ["a", "b", "c", "d", "e", "f",
"g", "h", "i", “j"]; let promise = Promise.resolve(); for (const id of dataIds) { promise = promise.then(() => openFile(prefix + `${id}.txt`)); } ίʔϧόοΫ openFile(prefix + "a.txt", (event, xhr) => { openFile(prefix + "b.txt", (event, xhr) => { openFile(prefix + "c.txt", (event, xhr) => { openFile(prefix + "d.txt", (event, xhr) => { openFile(prefix + "e.txt", (event, xhr) => { openFile(prefix + “f.txt", (event, xhr) => { openFile(prefix + "g.txt", (event, xhr) => { openFile(prefix + "h.txt", (event, xhr) => { openFile(prefix + "i.txt", (event, xhr) => { openFile(prefix + “j.txt", (event, xhr) => {}); }); }); }); }); }); }); }); }); }); ϓϩϛε
·ͱΊ 1SPNJTF ඇಉظॲཧ UIFO IPHF ʜ IPHF ʜ 1SPNJTF
ඇಉظॲཧ UIFO IPHF ʜ 1SPNJTF ඇಉظॲཧ UIFO IPHF ʜ ɾ ɾ ɾ 1SPNJTFΦϒδΣΫτΛ࡞Δ 1SPNJTFΦϒδΣΫτͷԾҾPO'VMpMMFE ͱPO3FKFDUFE ɹɹYISͱ͍͏ϦΫΤετΦϒδΣΫτΛ࡞Δ ɹɹ63-ͷϦιʔεΛ(&5͢ΔϦΫΤετ ɹɹϦΫΤετඈͯ͠σʔλ͕ϩʔυ͞ΕͨΒҎԼΛͬͯͶ ɹɹʢ͙͢Εͱݴͬͯͳ͍ʣ ɹɹɹɹϨεϙϯεͷςΩετΛίϯιʔϧʹදࣔ͢Δ ɹɹɹɹԾҾPO'VMpMMFEʹϦΫΤετΦϒδΣΫτ࣮ͯ͠ߦ͢Δ ɹɹɹɹʢڻዼͷਅ࣮ɿPO'VMpMMFEؔʣ 1SPNJTFΦϒδΣΫτ ໊લͷͳ͍ؔ ୈҾʢؔʣ ʢ͜Εઈରඞཁʣ ୈҾʢؔʣ ඇಉظతʹ࣮ߦ͍ͨ͜͠ͱ ɾ ɾ ɾ ྃ࣌ʹ࣮ߦ͍ͨ͜͠ͱ ɾ ɾ ɾ غ٫࣌ʹ࣮ߦ͍ͨ͜͠ͱ ɾ ɾ ɾ
αϯϓϧίʔυɺ(JU)VCʹ্͛·ͨ͠ IUUQTHJUIVCDPNDIJIBTPBTZODISPOPVT@TUVEZ ɹɹɹ݁ہࣗͰಈ͔ͯ͠ΈΔͷ͕Ұ൪ʂʂ
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ มͳͱ͋ͬͨ͜Βڭ͍͑ͯͩ͘͞ʂ
ࢀߟࢿྉ 1SPNJTFͱBTZODBXBJUͰ+BWB4DSJQUͷඇಉظॲཧΛγϯϓϧʹهड़͢Δ ɹɹIUUQTTCqOFUCMPHTJNQMJGZJOHBTZODDPEFXJUIQSPNJTFBOEBTZODBXBJU ॳΊͯͷ+BWB4DSJQUୈ൛ʕ&4Ҏ߱ͷ࠷৽Σϒ։ൃ ɹɹIUUQTXXXBNB[PODPKQEQ 1SPNJTFʹΑΔ+BWB4DSJQUඇಉظॲཧϨγϐू ɹɹIUUQTTCqOFUCMPHQSPNJTFDPPLCPPLJ