Asynchronous JavaScript #4 - Promises

Поделиться
HTML-код
  • Опубликовано: 25 окт 2024

Комментарии • 129

  • @12321vin
    @12321vin 8 лет назад +58

    I have gone through many tutorials.. but this one nailed it..

    • @NetNinja
      @NetNinja  8 лет назад +18

      BOOM. Thanks :)

    • @marangelomar
      @marangelomar 8 лет назад +5

      I agree!!! this one is really great!!

    • @IGEDEWICAKSANA
      @IGEDEWICAKSANA 6 лет назад

      I felt so, easy to learn as beginner's.
      Thanks

  • @junubiman276
    @junubiman276 7 лет назад +68

    Promise us you gon' keep making these tutorials!

    • @faisalkhurshid4032
      @faisalkhurshid4032 7 лет назад +34

      .then( "Upload those tutotials.").catch("What happend bro?")

    • @ShashotoANur
      @ShashotoANur 4 года назад +1

      That's gonna have to wait

  • @_worksonmymachine
    @_worksonmymachine 6 лет назад +3

    I've watched so many videos trying to explain promises and only after THIS one do I finally understand them. Thank you!!!

  • @not_enoughmana
    @not_enoughmana 8 лет назад +2

    This tutorial series has cleared up a great deal of confusion surrounding aynsc calls & promises! Thanks so much and please keep making them!

  • @temprmj1
    @temprmj1 6 лет назад

    Big props for taking the time to cover the inside story so we understand what's going on under the hood. You always provide just the right amount of detail, most videos are either provide too much detail or not enough.

  • @raghavendrap908
    @raghavendrap908 5 лет назад

    The first time I'm commenting to a tutorial, as it made me do. The best promises tutorial on the internet. Nailed it completely brother. Thank you very much.

  • @l441828872
    @l441828872 6 лет назад +2

    This tutorial series is brilliant, very clear!

  • @joshrochon6243
    @joshrochon6243 6 лет назад +3

    Wow, when i first subscribed a few months back, you had 80k subs. Excited to see this channel growing

  • @kirvelissariou
    @kirvelissariou 7 лет назад

    Great tutorial! The part where you say that promises are a placeholder for the result of an asynchronous action helped me fully understand the concept, as well as appreciate the usefulness of promises.

  • @maxnovikov51
    @maxnovikov51 8 лет назад +10

    thank you for explaining super hard things in super simple ways

  • @Bryan-zo6ng
    @Bryan-zo6ng 7 лет назад

    Good job brother. One of the better promise explanations on youtube. Great real world example.

  • @andrewking1173
    @andrewking1173 5 лет назад +6

    why are you better than my college professor... great explanation

  • @Masteblok
    @Masteblok 8 лет назад

    Thank you so much for this video. I was pretty frustrated today because of the callback hell and didn't quite understand promises before. This cleared so much for me in 13 min. :)

  • @oumaimabenjouida6724
    @oumaimabenjouida6724 4 года назад

    your the most good teacher i ever seen , thanks a looot

  • @sandy4799
    @sandy4799 6 лет назад

    Thank you very much Net Ninja ! Your tutorials have helped me a lot. This particular tutorial has clearly demonstrated promises.

  • @aur7062
    @aur7062 6 лет назад

    Thanks a lot for this tuto, promises looks much simpler now. Also, I like that you showed 2 ways to use them.

  • @gersoncjunior
    @gersoncjunior 7 лет назад +2

    Oh god! I'm so happy, finally I understood how promise works! Thank you!

  • @johnstowers4609
    @johnstowers4609 7 лет назад

    An excellent discussion on the topic of Promises. Thank you!

  • @yamacode9958
    @yamacode9958 5 лет назад +1

    Incredible videos, thank you. I have one suggestion please use a bigger font in the future as it can be hard to read on some screens.

  • @adante407
    @adante407 6 лет назад +2

    Magnificent! 👍 no async await? Very helpful tutorial. I understand your content better than any others it seems, even tutorials I have purchased. You have just got that knack man! I salute you. 😄

    • @NetNinja
      @NetNinja  6 лет назад +2

      Thanks man. I created this before the rie of async / await but I will be adding tuts on it in the future :)

    • @adante407
      @adante407 6 лет назад

      @@NetNinja Great! Thanks for the reply. 👌👍

    • @alittlebyte
      @alittlebyte 5 лет назад

      True story..

  • @natesh1
    @natesh1 6 лет назад

    Man that add with headspace is the best add ever!

  • @gracemwendem22
    @gracemwendem22 4 года назад

    made simple and easy to understand,a big thankyou!

  • @MrSuija
    @MrSuija 6 лет назад

    Definitely the best explanation on the topic.

  • @StyleTrick
    @StyleTrick 7 лет назад +4

    Thank you so much for these tutorials! I wish you the best for 2017 and hope you can expand your channel by tenfold!

  • @validnap
    @validnap 7 лет назад +4

    You are really ninja in the explanations :)

  • @Mathspy
    @Mathspy 7 лет назад

    Also I just wanna say I love your signature "If we can spell it"

  • @jaroslawtrzybinski
    @jaroslawtrzybinski 6 лет назад

    You're very good teacher. Thank You

  • @donnguyen5164
    @donnguyen5164 6 лет назад

    Nice example and explanation!

  • @samaneh77
    @samaneh77 6 лет назад +1

    amazing videos thanks :) It was funny my heart sank when you said `.next` by mistake, I was like omggg I did not get it, there is more to it! LOL and then you corrected that :DD

  • @MrGreenpaulo
    @MrGreenpaulo 6 лет назад

    You are great at explaining stuff mate, just been on your Patreon page and may well sign up in the future! Hope you get your $1000 a month so you can do this full time! Nice to hear a fellow countryman teaching as well ;)

  • @abhisheksaini2583
    @abhisheksaini2583 6 лет назад

    Awesome tutorial feeling enlightened :D. Keep up the good work

  • @Cforcode91
    @Cforcode91 8 лет назад +1

    Hi, it's a useful tutorial. You may also like to add the steps to pass data to the URL and handling the returned values in the jQuery way.

  • @this.channel
    @this.channel 5 лет назад +1

    Now I finally understand why and how fetch() returns a promise.

  • @igorkalaciov1306
    @igorkalaciov1306 4 года назад

    Lovely work mate, big fan of yours. As everyone has been saying - your content is definitely easier to understand and digest. Massive thank you for doing this.
    Wanted to ask you, is it possible and is it good practice to add error handling to the final $.get code that you show in this video? Or does jQuery .get method already has error handling built in?

  • @sethurk
    @sethurk 3 года назад

    Lovely tutorial as usual. In this case, 7:09 , can we say it is synchronized calls as they are sequential and waiting for the previous call to complete?

  • @cnicolasgr5245
    @cnicolasgr5245 8 лет назад +6

    Hey, The Net Ninja, I recently saw something really intersting : the parallax scrolling. Some websites use this trick and they look really nice could you make a tutorial ?
    I love your tutorials. (I'm french, pls don't blame me if they're mistakes)

    • @NetNinja
      @NetNinja  8 лет назад +9

      +Clem Nicolas Hey, that's a nice idea :). I will see what I can do someime soon!

    • @cnicolasgr5245
      @cnicolasgr5245 8 лет назад +1

      +The Net Ninja thx you !!!

  • @shabih001
    @shabih001 7 лет назад

    Thanks a lot for sharing this.. this tutorial is awesome. Subscribed and liked. Thanks a lot Sir. God bless. :)

  • @Hgayashan
    @Hgayashan 7 лет назад

    Really a great Tutorial :)

  • @TheGuroguro12
    @TheGuroguro12 7 лет назад +1

    You are brilliant, thank you very much.

  • @billpliske
    @billpliske 7 лет назад

    Great job Net Ninja. So if Promises are already built into jQuery, and it's that short of a code block .... why would you use the first method?
    Because you can't use jQuery?

  • @BennyPowers
    @BennyPowers 8 лет назад

    promises can also help you avoid blinking by lining up all our ducks and only firing the rendering functions when the final promise has resolved

  • @samersabri6068
    @samersabri6068 6 лет назад

    Do you need a catch for the jquery method? Just trying to check whether you left it out or whether you get it for "free." Great video btw!

  • @govardhangd9387
    @govardhangd9387 6 лет назад

    hey net ninja love your videos
    just wondering.... how long have you been a developer???

  • @gouravgosain8733
    @gouravgosain8733 6 лет назад

    nice and simple

  • @CroModder
    @CroModder 8 лет назад

    11:25 - Someone was working with generators before recording this video ;)
    Anyway, nice explanation.

  • @stepankolomiyets8807
    @stepankolomiyets8807 6 лет назад

    Can we suggest the argument 'url' to be an example of implementing closure in the context of this video?

  • @akashpjames987
    @akashpjames987 7 лет назад

    Do you have a github for these codes? If you have, can you put it in the description? BTW, your explanations are super clear and you are doing a great job. Subscribed to your channel.

  • @fariduzzamaansari1656
    @fariduzzamaansari1656 6 лет назад

    Hey, really nicely explained. I just had a question that with the parameters, in the functions like resolve, reject, tweets, error, xhr etc etc. Are these some kind of keywords we absolutely have to use to get the data? If they are not, then how does the function know to log the json data to the console when you pass anything to the function? Thanks.

  • @stephenpeters9125
    @stephenpeters9125 6 лет назад

    So I'm watching this, and this is all new to me. I've never had to use promises in the (seemingly simple) things I've built, would an appropriate scenario for promise use be when you're trying to get data from multiple sources/api's at once?

  • @davidconnelly1793
    @davidconnelly1793 7 лет назад

    Hey, I really appreciate these videos and it's very well explained. Thank you!
    Can I ask a question that is sort of related to this video?
    I've seen lots of nodeJS tutorials where people use an NPM library called Async. I cannot see what Async brings to the table that your method (shown above) cannot already handle.
    So, I'm trying to decide whether to use the NPM Async module (as I see in a few tutorials) or whether to use your method - which appears to be pure javascript.
    I'd be REALLY grateful if you could shine some light on this. Perhaps I'm missing something. Thank you!

  • @shankerm3959
    @shankerm3959 6 лет назад

    Excellent tutorial

  • @joshuadix9330
    @joshuadix9330 7 лет назад

    Why didn't you use fetch, it's a prebuilt promise for such a thing as doing an HTTP Request?

  • @mithuns8410
    @mithuns8410 6 лет назад

    Hi could u please explain Observables? Whether Observable comes under this asynchronous JavaScript category?

  • @pepeback
    @pepeback 8 лет назад

    Hi, thanks for your brilliant tutorials. In this one, I still dont get the use of "resolve" and "reject" arguments. They are supposed to be functions as they receive a value, but I dont see where it goes from there.

  • @Ferretrage8
    @Ferretrage8 7 лет назад

    Great series man. So sick of callback hell, had to learn a better way.

  • @samehabbadi8243
    @samehabbadi8243 6 лет назад

    الله يرزقك ما تتمناه

  • @DrNabeel20
    @DrNabeel20 4 года назад

    Thank you

  • @casey-zd5mj
    @casey-zd5mj 7 лет назад +2

    If you need a friend, don't look to a stranger. You know in the end, I'll always be there.

  • @esheakasinera4013
    @esheakasinera4013 5 лет назад

    I have a question: do you know how you would return values from a function (where in the function you are receiving the image dimensions from a url) and store that into global variables (preferably a global array)?

    • @esheakasinera4013
      @esheakasinera4013 5 лет назад

      I have objects and this is being done for each object as well

  • @hamzadadoune3852
    @hamzadadoune3852 8 лет назад

    Thank you for your videos, and i want to ask you if i have an XML file instead of JSON file, can i do resolve(XML.parse(xhttp.response)) to have my XML object (I have tried it but it's not working) what should i do?. Thank you again and good afternoon.

  • @ismahenelarbi7386
    @ismahenelarbi7386 6 лет назад

    How come do you display an object without using JSON.parse ?
    Thanks for this tutorial, you make everything easy and fun :)

  • @cedric_ds
    @cedric_ds 4 года назад

    If the first promise in the chain returns an error, will that error just be forwarded until the catch() at the end of the chain? And what will happen with all the promises in between? Will they get ignored?

  • @mujeexminiminer7653
    @mujeexminiminer7653 6 лет назад

    So you don’t need the .catch handler if you use the jquery interface¿

  • @tigere01
    @tigere01 5 лет назад

    If you can console.log() the successful request data, how can you combine it all into a single Object or single Array??

  • @objectsobjects4424
    @objectsobjects4424 7 лет назад

    What I'm not sure of: Does it make sense to use $.get() after using an AJAX call? They don't seem to be similar in that respect and I have a ton of ajax calls from coming live websites via API.

  • @banzobeans
    @banzobeans 6 лет назад

    3:16 Can someone please explain:
    I'm assuming the first function parameter in a .then() statement always represents the returned result of the promise and could be named anything we want.
    I'm assuming we are free to add more parameters to this kind of callback function, but the first parameter is reserved for the returned result of the promise.
    Are my assumptions correct?

  • @MrRossss1
    @MrRossss1 6 лет назад

    Hi, is there a way this could be used with a loop say to get the values from an array and use each value to form the requests, so you could make a request for each item in the array? Thanks

  • @rohitgoyal9883
    @rohitgoyal9883 6 лет назад

    In asynchronous javascript, when we get data from outside the javascript then that data will always show at last after trigger all the thread.??

  • @simon.p
    @simon.p 7 лет назад

    Is there a reason that I can't just simply make an array of the file paths and loop them over the $.get().then() function?

  • @Neroshkiet
    @Neroshkiet 7 лет назад +1

    There is one god damn thing that I can't understand, its the parameter of the callback function:
    If i understand correctly, a paremeter is just a placeholder for whatever argument we want to pass in the function. How does it know that the parameter "tweets" refers to whats in the "tweets.json" file. The name of the parameter is up to me, I can call it "tweets" or whatever I want, but how does it know what to pass in > How is "tweets" replaced by the content of the "tweets.json" file? By what argument the "tweets" parameter is getting replaced?
    $.get("data/tweets.json")
    .then(function(tweets){
    console.log(tweets);
    });
    Its probably dumb, but I can't visualize it.

    • @jorisbonson386
      @jorisbonson386 7 лет назад

      The parameter to that particular callback function is passed by resolve(JSON.parse(xhttp.response)); in the previous execution of 'get', which was get("data/tweets.json");, hence we know it's this data we're getting as we've explicitly requested it.

    • @abhiroopcvk3780
      @abhiroopcvk3780 6 лет назад

      Not sure if you still need a reply, but going to reply anyways for anyone else who chances upon this video and has the same question as you did.
      The tweets parameter can be called anything else, and it doesn't replace 'tweets.json', rather is a placeholder for the data that is returned by the 'get' request.
      In our case, the 'tweets' parameter happens to be a json. It could also be text. It could also be a boolean, or a string or any other data type. How you wish to handle that data type is up to you in your callback function!

  • @cedric_ds
    @cedric_ds 4 года назад

    What are "resolve" and "reject", where are these functions declared??

  • @MrVisheshsingh
    @MrVisheshsingh 7 лет назад

    YOU ARE GOD _/\_ Thanks a lot

  • @ladyking83
    @ladyking83 4 года назад

    so with `$.get()` how do we handle errors? i didn't see you writing error handling code..

  • @hyperborean72
    @hyperborean72 4 года назад

    Why do we have to return from the 2nd and 3rd calls to promise (get())? Promise has return inside so they look like a return from return

    • @NetNinja
      @NetNinja  4 года назад

      Hey, the return allows us to chain .next( ) again because the callback in itself returns a promise.

  • @standinshd
    @standinshd 6 лет назад

    subscribed

  • @VjrjS
    @VjrjS 6 лет назад

    One subscriber for you!!!

  • @antoniosalzano5253
    @antoniosalzano5253 4 года назад

    what is the difference between then and resolve? i can't understand..

  • @returnedfaith7254
    @returnedfaith7254 6 лет назад

    The whahh has it place in my mind.

  • @connectgfv6775
    @connectgfv6775 4 года назад

    the .catch in jQuery?

  • @stephenpeters9125
    @stephenpeters9125 6 лет назад +1

    Anyone know if promises are more widely supported in browsers these days?

    • @lardosian
      @lardosian 6 лет назад

      Yes unless you use opera or IE11. Caniusedotcom is great for checking anything like this .

  • @knsense
    @knsense 8 лет назад +1

    With the jQuery method there's no need to handle errors?

    • @eahofer
      @eahofer 8 лет назад +3

      In checking the documentation (starting at api.jquery.com/jQuery.get/ , you'll see that Promise is a deferred object, so --> api.jquery.com/category/deferred-object/ and from there, there's no "catch", but I found 2 options:-
      api.jquery.com/deferred.then/ - and this has a 2nd function to call on failure
      api.jquery.com/deferred.fail/ - this can be chained to, but, you still get an ugly message
      Perhaps Shaun could comment?

    • @marpupradeep220
      @marpupradeep220 6 лет назад

      I am having the same question...please respond @the net ninja?

  • @andrewobrigewitsh
    @andrewobrigewitsh 6 лет назад

    Use Axios.

  • @soujanya1234
    @soujanya1234 6 лет назад

    hey if any one could explain me the real world scenario of promises and generaters where can we us it . that would be really appricetaed

  • @eldinedy6837
    @eldinedy6837 5 лет назад

    what is future of jQuery??

    • @NetNinja
      @NetNinja  5 лет назад

      Not a bright one, anymore.

    • @eldinedy6837
      @eldinedy6837 5 лет назад

      @@NetNinja sadly. Promises much easyer with jquery. Shorter code

  • @armyofone2323
    @armyofone2323 5 лет назад +1

    Huaaaaaa

  • @abdulrehman-rt5mz
    @abdulrehman-rt5mz 7 лет назад

    how this $.get() jquery method is returning promise object pls some1 help me out plssss

    • @johnstowers4609
      @johnstowers4609 7 лет назад +1

      jQuery.get() simply encapsulates the same code that "The Net Ninja" wrote - his function named get(). : )

    • @abdulrehman-rt5mz
      @abdulrehman-rt5mz 7 лет назад

      thanks ;)

    • @johnstowers4609
      @johnstowers4609 7 лет назад

      You're welcome. An "excellent" source for questions and answers is stackoverflow.com! : )

    • @abdulrehman-rt5mz
      @abdulrehman-rt5mz 7 лет назад

      haha yap i know i do go through stackoverflow :P

  • @davideugene7911
    @davideugene7911 6 лет назад

    " Promises Hell " ????!!! :-D

  • @returnedfaith7254
    @returnedfaith7254 6 лет назад

    Why does the hell promise seem to me more like a hell than the hell you said?

  • @jdevcast6527
    @jdevcast6527 7 лет назад

    promising

  • @Earlosaur
    @Earlosaur 7 лет назад +6

    "bob's you're uncle"
    -the net ninja 2017

  • @f7a996
    @f7a996 5 лет назад

    Great tutorial. I was hoping to see error handling at the end of the jQuery methods. .fail worked for me
    window.onload = function(){
    // the jQuery way
    $.get("data/tweets.json").then(function(tweets){
    console.log(tweets)
    return $.get("data/friends.json")
    }).then(function(friends){
    console.log(friends)
    return $.get("data/videos.json")
    }).then(function(videos){
    console.log(videos)
    }).fail(function(error){
    console.log(ErrorEvent)
    })
    }

  • @jbaru1211
    @jbaru1211 6 лет назад

    Doesn't work

  • @romasbitinas643
    @romasbitinas643 2 года назад

    Здравствуйте россияне

  • @qemperusqestimad3335
    @qemperusqestimad3335 4 года назад

    outdated lessons, non-working
    Access to XMLHttpRequest at 'file:///.......tweets.json' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

  • @mustafaanwar463
    @mustafaanwar463 7 лет назад +5

    Sir please speak slowly 😭😭😭

    • @matthew78uk
      @matthew78uk 7 лет назад +8

      Mustafa Anwar you can decrease the speed of the video (and the voice) by clicking the small cog icon on the bottom right of the youtube window

    • @modoaberto8878
      @modoaberto8878 5 лет назад

      Shift +

  • @fxstreamer238
    @fxstreamer238 5 лет назад

    whats the point of using promise if you are not gonna get all the 3 data asynchronously? thats so absurd to me to use promise and .then in this example. you could just use regular request if you wanted to synchronously get data sequentially AFTER (.then) the first request finished. so stupid. in this example you are only using 1 thread. unfortunately all the tutorials on youtube missing the point of using promise and making the same dumb example just copy paste other youtubers without knowing whats going on

    • @NetNinja
      @NetNinja  5 лет назад +1

      The point being made is that you can write asynchronous code to fetch multiple sets of data in a synchronous way. This could be, for example, using a couple of different apis, where the request of one is dependent on the response from another. So async requests can be made synchronously if needed. And it’s still non-blocking. The rest of any code in the file could still run while this happens That’s not always the case, and sometimes a single promise is enough instead of chaining. But that is the idea behind chaining promises. I hope this helps you understand.

    • @fxstreamer238
      @fxstreamer238 5 лет назад

      @@NetNinja yes i figured out that the rest of the code runs on a different thread if applied but we dont see here or never mentioned. it was just so confusing to me first time i watched multiple tutorials to understand that. the thing is the example used many (.then) syntax which is unnecessary and counter intuitive and ive seen that out there its like a ritual between programmers. because the "friends" data we get or the "videos" dont depend on the result of the first data("tweets") and the whole point is missing we could write this code asynchronously AND use (.then ) for print the results to show sync and async aspect of it. Thanks for your response