All language subtitles for 14. Asynchronous JavaScript Promises

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic Download
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,790 --> 00:00:07,030 So to finish this section, we're quickly going to review how we can use asynchronous JavaScript techniques 2 00:00:07,030 --> 00:00:09,040 in order to fetch data. 3 00:00:09,070 --> 00:00:13,270 So to load data from an external web API. 4 00:00:13,300 --> 00:00:20,020 And the first way in which we're going to do this is to directly use the concept of promises. 5 00:00:21,150 --> 00:00:26,160 And let's start by deactivating all this code that we no longer need. 6 00:00:27,330 --> 00:00:34,500 So now we're actually going to leave this domain of books and we'll just focus quickly here on something 7 00:00:34,500 --> 00:00:35,610 entirely different. 8 00:00:36,260 --> 00:00:43,550 So in order to fetch data from an API in the browsers, we have the so-called fetch API. 9 00:00:44,110 --> 00:00:52,450 So a function that is called fetch and into which we can pass a URL of an API and let's actually use 10 00:00:52,480 --> 00:01:00,010 a kind of dummy API that you commonly see in tutorials and that's the Json. 11 00:01:00,700 --> 00:01:02,200 Place holder. 12 00:01:03,110 --> 00:01:03,770 An API. 13 00:01:05,230 --> 00:01:05,590 Yes. 14 00:01:05,590 --> 00:01:06,640 It doesn't matter. 15 00:01:07,340 --> 00:01:10,490 So just google this Json placeholder. 16 00:01:11,420 --> 00:01:12,080 All right. 17 00:01:12,080 --> 00:01:18,770 And then open up this this page, and then we can just copy this URL right here. 18 00:01:19,660 --> 00:01:22,300 So slash to dos, but without the one. 19 00:01:22,300 --> 00:01:23,650 So that's not necessary. 20 00:01:23,650 --> 00:01:27,700 And immediately here you also see that they are using the fetch function. 21 00:01:27,700 --> 00:01:28,390 Right? 22 00:01:29,360 --> 00:01:33,920 So let's create a new string like this and then paste in that URL. 23 00:01:34,490 --> 00:01:42,080 Now, in order for this to actually work in Quokka right here, you need at least version 18 of NodeJS 24 00:01:42,080 --> 00:01:43,840 installed on your computer. 25 00:01:43,850 --> 00:01:51,080 So if you didn't do that previously, when I mentioned that you should use version 18, then this is 26 00:01:51,080 --> 00:01:58,010 a good time to update your NodeJS version because Quokka here actually isn't using a browser, but it 27 00:01:58,010 --> 00:02:03,950 is using NodeJS, and the fetch function was only added to Node in version 18. 28 00:02:05,080 --> 00:02:05,530 Okay. 29 00:02:05,530 --> 00:02:10,840 But anyway, what is going to happen here when this function gets called? 30 00:02:10,870 --> 00:02:19,300 Because fetching the data from this API here, so from this string will of course take some time because 31 00:02:19,300 --> 00:02:26,530 JavaScript needs to do an Http request, needs to wait until that request is processed and then it needs 32 00:02:26,530 --> 00:02:30,330 to download that data basically from this server. 33 00:02:30,340 --> 00:02:34,240 And so in the meantime, JavaScript actually keeps running. 34 00:02:34,420 --> 00:02:37,240 So if we log something to the console here. 35 00:02:40,370 --> 00:02:44,630 Jonas, for example, it will get executed immediately. 36 00:02:44,630 --> 00:02:48,970 So JavaScript will not wait until this data is fetched. 37 00:02:48,980 --> 00:02:55,190 It will simply execute this function and will then immediately move on to the next line of code, which 38 00:02:55,220 --> 00:02:56,840 in this case is this one. 39 00:02:57,050 --> 00:03:04,640 So how do we basically wait for this code here and do something as soon as the data has arrived? 40 00:03:04,940 --> 00:03:10,070 Well, that's where the asynchronous JavaScript techniques come into play. 41 00:03:10,100 --> 00:03:17,900 So this fetch function here, what it returns immediately and then moving on to the next line is a so-called 42 00:03:17,900 --> 00:03:18,770 promise. 43 00:03:19,160 --> 00:03:23,960 And we should be able to see that with a console dot log here. 44 00:03:25,170 --> 00:03:26,100 Let's see. 45 00:03:26,100 --> 00:03:29,730 And indeed, so this is a so-called promise. 46 00:03:29,730 --> 00:03:35,640 And it says here that it's pending because that's one of the multiple states in which a promise can 47 00:03:35,640 --> 00:03:36,060 be. 48 00:03:36,090 --> 00:03:40,230 So it can be pending when it's still doing something in the background. 49 00:03:40,260 --> 00:03:42,150 It can be rejected. 50 00:03:42,180 --> 00:03:48,450 If there was an error in this case, if there was an error fetching this data, or it can be fulfilled, 51 00:03:48,450 --> 00:03:51,810 which means that the data is successfully arrived. 52 00:03:51,810 --> 00:03:58,170 And once again, I will not go deeply into the details here For that, you can go ahead and read the 53 00:03:58,170 --> 00:04:04,770 Promise documentation on mSDN, or you can watch some YouTube video or you can of course also check 54 00:04:04,770 --> 00:04:06,810 out my complete JavaScript course. 55 00:04:06,810 --> 00:04:13,230 But anyway, the promise state that we are interested in here is the fulfilled state, right? 56 00:04:13,230 --> 00:04:21,150 So that's the state where the data has actually arrived and we can handle that state by using or by 57 00:04:21,150 --> 00:04:23,460 adding the then method. 58 00:04:24,070 --> 00:04:28,840 So let's, of course, first get rid of this console dot log. 59 00:04:29,080 --> 00:04:33,220 And so then again, this returns a promise. 60 00:04:33,220 --> 00:04:36,960 And on a promise we can call the then method. 61 00:04:36,970 --> 00:04:42,100 And so the then method will basically be called as soon as the promise is fulfilled. 62 00:04:42,100 --> 00:04:46,390 So as soon as it's successfully got back with the data. 63 00:04:46,390 --> 00:04:51,730 Now promises are used for more than data fetching, but here I'm just using that as an example. 64 00:04:51,850 --> 00:04:52,420 Okay. 65 00:04:52,810 --> 00:04:58,750 Now into this then method we need to pass in a callback function, which is basically the code that 66 00:04:58,750 --> 00:05:02,320 will then be executed as soon as the data has arrived. 67 00:05:03,730 --> 00:05:10,560 So JavaScript will call this callback function here with the response that it has received. 68 00:05:10,570 --> 00:05:17,800 So that's why I like to call this here Rez and this data then needs to be converted from Json to a JavaScript 69 00:05:17,800 --> 00:05:18,570 object. 70 00:05:18,580 --> 00:05:21,550 So again, not going deep into it here. 71 00:05:22,590 --> 00:05:25,590 And here it just needs to be res dot Json. 72 00:05:27,390 --> 00:05:27,960 Okay. 73 00:05:27,960 --> 00:05:33,180 And now the thing is that actually doing so will return another promise. 74 00:05:33,540 --> 00:05:37,050 So this Json here also takes some time. 75 00:05:37,050 --> 00:05:42,720 So it's also an asynchronous operation and therefore here we need to add another then. 76 00:05:45,380 --> 00:05:46,070 Okay. 77 00:05:46,430 --> 00:05:52,670 But then here we already have our final data in the JavaScript form. 78 00:05:52,670 --> 00:05:58,400 And so this function here will then get called with the result of this previous function, which is 79 00:05:58,400 --> 00:05:59,180 then the data. 80 00:05:59,180 --> 00:06:03,320 So let's simply log this data then to the console at this point. 81 00:06:04,920 --> 00:06:05,750 So let's see. 82 00:06:05,760 --> 00:06:09,180 And indeed, here we got the todos. 83 00:06:10,680 --> 00:06:15,960 So there's a lot of them and all of them came through the Json placeholder API. 84 00:06:16,080 --> 00:06:21,150 Now what you notice here again is that it first printed Jonas to the console. 85 00:06:21,150 --> 00:06:29,970 So the result of this, and only later is when this array of todo objects was finally printed to the 86 00:06:29,970 --> 00:06:30,900 console as well. 87 00:06:30,900 --> 00:06:34,050 And again, that's because this data arrived later. 88 00:06:34,800 --> 00:06:41,520 So what happened here was that, as I mentioned before, is that this fetch function fired off a request 89 00:06:41,520 --> 00:06:47,790 to the API and then immediately JavaScript moved on to the next line, which is this one here, where 90 00:06:47,790 --> 00:06:54,270 it simply basically registered this function here to be executed later, the same here, and then it 91 00:06:54,300 --> 00:06:56,760 immediately executed this line of code. 92 00:06:56,970 --> 00:06:57,780 Right? 93 00:06:57,900 --> 00:07:01,710 Then some time passed and then the data from the API arrived. 94 00:07:01,710 --> 00:07:08,940 And so then JavaScript goes back here and executes this callback function that we had registered before. 95 00:07:08,940 --> 00:07:11,490 And so this is where the response arrives. 96 00:07:11,520 --> 00:07:17,820 It's converted to Json and then here we receive the data in this callback function and log it to the 97 00:07:17,820 --> 00:07:18,540 console. 98 00:07:18,540 --> 00:07:25,350 So literally JavaScript will wait until it executes these functions here, which is usually not how 99 00:07:25,380 --> 00:07:30,000 JavaScript works, but here we are using asynchronous JavaScript. 100 00:07:30,030 --> 00:07:36,970 While usually JavaScript is a synchronous language where one line of code is simply executed after the 101 00:07:36,970 --> 00:07:37,540 other. 102 00:07:37,960 --> 00:07:38,590 Okay. 103 00:07:38,590 --> 00:07:42,670 And here now at this point you could do something a bit more meaningful. 104 00:07:42,670 --> 00:07:49,390 For example, in React, we would then typically take this data and set some state which would then 105 00:07:49,390 --> 00:07:52,510 get reflected right in the user interface. 106 00:07:52,510 --> 00:07:55,150 But of course more about that later. 107 00:07:55,150 --> 00:08:03,100 For now, I just wanted to basically show you or to review how we use these promise methods here in 108 00:08:03,100 --> 00:08:08,530 order to successfully handle the moment when the data arrives from the API. 10552

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.