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
Tauriでネイティブアプリを作りたい
tsucchinoko
0
350
みんなでプロポーザルを書いてみた
yuriko1211
0
190
WEBエンジニア向けAI活用入門
sutetotanuki
0
330
PLoP 2024: The evolution of the microservice architecture pattern language
cer
PRO
0
2.4k
僕がつくった48個のWebサービス達
yusukebe
20
17k
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
160
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
440
リリース8年目のサービスの1800個のERBファイルをViewComponentに移行した方法とその結果
katty0324
5
4.2k
Java ジェネリクス入門 2024
nagise
0
690
【Kaigi on Rails 2024】YOUTRUST スポンサーLT
krpk1900
1
310
JaSST 24 九州:ワークショップ(は除く)実践!マインドマップを活用したソフトウェアテスト+活用事例
satohiroyuki
0
440
デプロイを任されたので、教わった通りにデプロイしたら障害になった件 ~俺のやらかしを越えてゆけ~
techouse
53
34k
Featured
See All Featured
The Pragmatic Product Professional
lauravandoore
31
6.3k
GitHub's CSS Performance
jonrohan
1030
460k
BBQ
matthewcrist
85
9.3k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
A Tale of Four Properties
chriscoyier
156
23k
Agile that works and the tools we love
rasmusluckow
327
21k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Building an army of robots
kneath
302
42k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
How to Ace a Technical Interview
jacobian
276
23k
Teambox: Starting and Learning
jrom
133
8.8k
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