Understanding Synchronous vs Asynchronous JavaScript

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

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

  • @justinbenson3045
    @justinbenson3045 3 года назад +11

    Whenever I'm stuck on something with JS and I see a video about the concept with your name by it, I breathe a sigh of relief. Thanks for all you do!

  • @rotrose7531
    @rotrose7531 4 года назад +3

    If anybody wants to learn javascript seriously, this is one-stop channel that can guarantee to satisfy anyone with top notch quality. As always thank you.

  • @dolanbright3733
    @dolanbright3733 4 года назад +7

    omg only video that actually helped me learn async js. love you and you made the world a better place. i cant express how thankful i am

  • @somtovitus
    @somtovitus 2 года назад +1

    I have watched tons and tons of videos on this subject. Only yours cleared my confusion. I don't feel satisfied if I fail to say that you explain things in a manner many don't.

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

    solid examples, comparisons, and commentary. keep up the good work boss!

  • @vinisantos.
    @vinisantos. 4 года назад

    I've been searching all around trying to get the answer for an issue I'm having - I'm making a DB call and trying to do something with the result but it keeps returning only after the action has already been taken - and I found all sorts of possible complex solutions that didn't really lead me anywhere. I had already lost hope and you provided me with the solution.
    It's simple, straight to the point and very well explained.
    Really appreciated! I subscribed for that reason alone!

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

    Hi Steve Griffith. Thanks for this clear explanation. Examples/demo always help understanding things. I always wondered that why sometime javascript works asynchronously and sometimes synchronously. Now it is almost clear. People like you on youtube and other platforms are making our lives easier! Can't thank you enough! :)

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

    This is a great video explaining Synchronous vs Asynchronous in JavaScript. Save it to your lists.

  • @OptimalTrainingGroup
    @OptimalTrainingGroup 5 лет назад +2

    Sir, this is the best explanation of asynchronous on the whole internet. Your explanation and example were spot on. Thank You! SUBSCRIBED!

  • @dzentsetsu5607
    @dzentsetsu5607 3 года назад +1

    Steve I'm sure you get this a lot, but I'm gonna say it anyway. Your content makes me love learning. As a professor you achieved highest possible "skill level", you make people fall in love with subject you teaching, it's priceless.

  • @eicoz
    @eicoz 3 года назад +1

    This is the best video about Synchronous and Asynchronous code concepts, I have ever seen. You are very good at teaching, Thanks a lot !

  • @Sebeand
    @Sebeand 6 лет назад +4

    Ty for the video, very clear :) Keep it going. As a matter of fact, I spend 2 hours reading an article and I understood way better form your vid.

  • @SithaSek
    @SithaSek 6 лет назад +18

    Simple but very informative !!! Thank you!

  • @easterntornado
    @easterntornado 5 лет назад +2

    You drive the point very simple & clear @Steve Griffith !!! Thanks for such a nice explanation.

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

    Simple yet Excellent explanation. THANKYOU!!

  • @LeetCodeSimplified
    @LeetCodeSimplified 3 года назад +1

    Thanks a lot, man! I learned so much from this video! You are truly the best teacher!!!

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

    @13:06:
    I put the same code in CodeSandbox - but unlike in your console, ma setTimeout(log(p) still throws an 'Promise pending' isntead of 'Promise resolved'.
    Question:
    Is it because of CodeSandbox or did Javascript change during the years?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 года назад

      JSBin and CodeSandbox don't run code in the same way as if you run it in the Terminal with NodeJS or in the browser directly.

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

    Since promise and setTimeout both are async function then why is promise is running before setTimeout when promise is written after setatimeout

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 года назад

      The Promise object is created synchronously. The result of the Promise is the thing that is async.

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

    Awesome content. You explained in a simple but yet effective way. Thanks

  • @LukmanSuprayitno
    @LukmanSuprayitno 5 лет назад +3

    set aside, let the synchronous finished, and re-run. that's the key :)
    thank you, now I can continue to learn more javascript :D

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

    Amazing explanation! Thank you for humbly sharing your extensive knowledge.

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

    Crystal clear explanation :-).Please adding more videos on java script concepts.Than you so much.

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

    13:13 can I ask a simple question?
    why on line 53 we're getting resolved, and on line 49 we're getting pending. and we're using the same function on both which is (setTimeout)
    shouldn't we get resolved on line 49 since we're resolving it?

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

      Oh, I figured it out after watching the Promise series. (we have to use .then to resolve it)
      I've been struggling with these async concepts for months. now I get it. thank you, Steve. Your videos are always the best.

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

    Best explanation that i found on RUclips thank you sir.

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

    So what does it mean when you get a call and caller if says async?

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

    You are a fantastic teacher. Truly fantastic

  • @bmzaron713
    @bmzaron713 4 года назад +4

    when genius meets humility

  • @velifurkanturkoglu1387
    @velifurkanturkoglu1387 8 месяцев назад

    is this concept the same for every other language? For example c#, does it work exactly the same or is there a difference in sync and async concept on different languages?
    Thanks for the videos, professor

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  8 месяцев назад

      The event loop and async / sync programming here is specific to JavaScript. Other languages like Dart or C# have asynchronous concepts but they each have their own approaches.
      Here is more about the JS sequence - ruclips.net/video/w-jULX0D5cg/видео.html

  • @javierarturorizzosudario.8505
    @javierarturorizzosudario.8505 3 года назад

    Thank you very much, Steve. Learned a lot from this tutorial !

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

    Please explain clearly what you mean by "set aside to let the synchronous stuff finish first"? Does that mean the setTimeout lines are literally skipped until the other lines of code have finished running, after which the code goes back to the setTimeout lines and executes them in their respective order??? Is that what's happening?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 года назад

      Here is a video I did years ago on setTimeout and other time functions - ruclips.net/video/0VVJSvlUgtg/видео.html
      When I say set aside, I mean that the function that is passed to setTimeout is put into a waiting area. It waits there until the time passed to setTimeout has expired. Then it is moved to a pending area that waits for an opportunity to be run. It is allowed to run once all the code on the main thread has completed.

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

    is there any way to run lets say setTimeout function right away and not put it aside? Like in you example:
    let a3 = 100;
    setTimeout(function(a3++)=>{
    }, 1000);
    console.log(a3);
    If I want to increment a3 every second right away before the next line of code runs, is there any way?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  6 лет назад

      No. setTimeout is an asynchronous function. It will always be set aside to be run later. Even if you set the time to zero, it will still be set aside.

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

    Hi Steve, is there a way to make your own custom asynchronous functions like settimeout? I know you can do it with async but i was wondering how it was done before that was introduced? I'm not sure how to start something running inside a function, have it complete the other code outside the function in the mean time and then go back to the call back after. Thanks

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  5 лет назад +1

      Promises is the basic structure that you can use to make your own async functions. Somethings in HTML5/JS are async by nature. XMLHttpRequest was one of the early ones along with setTimeout. With HTML5 a lot more were added - geolocation, localStorage, database, file access, etc. Callback functions are basically asynchronous. Events can be thought of as asynchronous. Anything that is off the main stack is basically asynchronous. The new async await is just a Promise wrapper around things.

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

      @@SteveGriffith-Prof3ssorSt3v3 Thanks Steve, much appreciated.

  • @mr.techno-era7474
    @mr.techno-era7474 6 лет назад

    I have developed web app which heavely used synchronous xmlhttprequest ( ajax) call. I am planning to deploy web on local server. Will my code break down near future when major browser stop supporting synchronous call? Please help, i can't find right forum to discuss it

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  6 лет назад

      Yes. Synchronous support with xmlhttprequest is being removed. It is already limited. You should update your code to use Promises through fetch( ). With the async - await feature added in ES6, you can create synchronous handling of the requests, if it is really required.
      A better practice is to design your web site/app to work without locking the user interface (which synchronous calls do). You can display indicators that information is being loaded. You can let the user continue to use the page while they wait.

    • @mr.techno-era7474
      @mr.techno-era7474 6 лет назад

      @@SteveGriffith-Prof3ssorSt3v3 thanks, what if i used old browsers? is it still work or not? I am using local server.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  6 лет назад

      @@mr.techno-era7474 IF you are using old browsers then you are using the old JavaScript engines that come with them. Therefore the old code can still run. However, old browsers eventually stop being supported. If you ever have a reason to replace your computer (infected with a virus, hardware failure, etc) you won't be able to get the old browsers any more.
      I would urge you to move away from the old technology, the old xmlhttprequest, and jQuery. The sooner you do it then the sooner you won't have to worry about being in a situation where your site will stop working.

    • @mr.techno-era7474
      @mr.techno-era7474 6 лет назад

      @@SteveGriffith-Prof3ssorSt3v3 Ok but why are they suddenly removing it? If someone doesn't drive bus well then we teach them, not instruct them to fly airoplane instead... Replacing code with asychronus call cause me really pain.. But, there is no solution, right.. ( no backward compatibility, right??)

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  6 лет назад

      This change away from xmlhttprequest and the security issues that it has started about 8 years ago.
      The changes are being made for good reasons. We replaced horses and carriages with automobiles. Now gas engines are being replaced with electric engines. Changes will always happen, it is just the pace of change that is increasing. The new methods are not just there to have something new. They were built to be better.

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

    Thank u . Finally i realised the idea of async properly .

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

    So I've been learning html, css, and javascript, I even have a noob portfolio. I'm going through your videos and preparing for interviews. If you can help me in any way or direct me somewhere please do. Thank you for explaining content man. I appreciate you Steven...

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

    Hi Steve, Thanks for the wonderful explanation. I have one query, Javascript keeps aside the async call from the main thread while execution. In this case, Promises are also async then why it executes before the setTimeout function gets executed.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  5 лет назад +1

      The setTimeout doesn't get called until the Promise runs. p becomes a container for a pending Promise. The Promise is a container for whatever code you want. The Promise runs in order to be resolved or rejected. At that point the setTimeout gets put aside as an async operation. When the setTimeout finishes its delay it tries to run its internal function. This internal function calls the resolve function for the Promise. The Promise inside p is now resolved and will pass its return value to the then( ) method of the Promise (if there is one)

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

    I like this man much much cux he bring everything unique and precious stuff

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

    Love it!! So help full in understanding why the logging is all over the place in my test code.

  • @Sam-cb3ye
    @Sam-cb3ye 5 лет назад +1

    Very well explained 👌👌

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

    omg. thank you so much. ive read 5 different blogs and watched 3 other videos and this is the only one that made sense to me :D

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

    I finally understood asynchronous code! Thank you so much!

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

    Great video, but my man...did you record this in the library??? You can talk louder

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  5 лет назад

      My old videos were with a poor mic. All the new ones are much better

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

      @@SteveGriffith-Prof3ssorSt3v3 Understood, I'm still giving you a thumbs up, subscribing and telling all my friends how awesome your vids are.

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

    Thanks Steve! Very good explanation. By the way, do "let" and "const" keywords hoist to the top? I thought declaration of these stays on the same line

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  6 лет назад +1

      All JS variables hoist to the top of their scope. I have a video on let and const if you need help on their scoping

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

      @@SteveGriffith-Prof3ssorSt3v3 Okay, thank you!

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

      @@SteveGriffith-Prof3ssorSt3v3
      Thanx for the good explanation! One mention regarding let variables:
      Acording to w3schools and othe sources to Variables defined with let are not hoisted to the top.
      Using a let variable before it is declared will result in a ReferenceError.
      The variable is in a "temporal dead zone" from the start of the block until it is declared:
      Example
      // you can NOT use carName here
      let carName;
      Source: www.w3schools.com/js/js_let.asp

  • @jbrabec6811
    @jbrabec6811 Год назад

    I might not be a smart man, but I know what Steve is - a damned good teacher!

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

    Hi Steve,
    How about async & await in this case ?Could you do another video? I've read in MDN docs the syntax could be more simpler than using promises in some cases.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  6 лет назад

      I will be doing videos on Async, Await with and without Promises. Not sure exactly when but in the next couple months.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  6 лет назад +1

      I've made a couple on async and await this summer. If you search for "async' from my channel home page you will find them.

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

    omg so helpful, I was gonna freak out when I was having so many problems with my JS debugging with JSON api's

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

    Simply superb, You are a good teacher...

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

    Great video, But I have a question, so sync code runs in order then is the async code also runs in order after the sync finishes executing ? meaning are all the async code runs on the same thread so it execute in order?

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

    Another very cool tutorial !

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

    thank you! i am having problems understand the concept and now I somewhat got it

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

    @steve Griffith : please i need help with Unit Testing - Jamsine ,
    and unit testing in general
    I'm new to the topic in general and i am kinda lost at their Site and their documentation don't know where to start .
    any videos , articles and resource will help.
    thanks a lot :)

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  5 лет назад

      I don't currently have anything on Jasmine. Please add it to my tutorial request video or upvote people asking for the same thing.

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

    Keep going man you're doing a great job
    thank you very much

  • @MDSakib-hz1kh
    @MDSakib-hz1kh 4 года назад

    Tnq u sir. Love from Bangladesh

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

    That was a very good explanation! Thanks a lot!

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

    Super clear!! methodical!! You rock!

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

    Steve, thank you very much for a simple and well thought out example and your explanation. You are the best after Tony Alicia's videos on JavaScript.
    I think you should put a course on Udemy. Let me know if you have a patreon account.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 года назад +7

      Thanks. I dont have a patreon account. I do have a Udemy account but decided to keep posting my videos here for free for everyone

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

    perfect explanation. would love to learn from you man.

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

    Is there a way or tools to Asynchronous javascript files for a non-developer?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  6 лет назад

      Asynchronous code is not a thing that you do. It is a feature of the language. To use asynchronous features you need to program them. It is development work. If you want to program then you need to learn how to program. There is no shortcut.

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

      @@SteveGriffith-Prof3ssorSt3v3 Thanks steve :( well, it seems heck of a task learning programming. I would rather rely on developer support for now.

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

    Nicely thaught , understood everything 👍👍

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

    Really good explanation, thanks!

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

    Very well explained

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

    excellent video, crystal clear

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

    how can i write async function without
    ---async await

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  5 лет назад +1

      There are things in JS that are asynchronous - Promises, Fetch, setTimeout, localStorage, DB, etc. You don't choose to make them async they just are. Async - await is to be able to treat async things in a synchronous way. Without async-await you need to use the then( ) for Promises or the callback functions for the others.

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

      @@SteveGriffith-Prof3ssorSt3v3
      if i want write asynchronous code in java script , how can i write ?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  5 лет назад +1

      @@talhabilal4069 you just use the methods that are asynchronous - promises, fetch, setTimeout, setInterval, web storage, file system, database, geolocation, things with callbacks. Each has it's own code. You need to learn each object.
      If you want to turn something else asynchronous then wrap it in a promise or an event listener

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

      @@SteveGriffith-Prof3ssorSt3v3 thanks

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

    really... great and very well understandably video ..!! but is there any rule Synchronous code run first and Asynchronous later?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  6 лет назад

      It's not a rule, it has to do with how JavaScript handles the event loop. There is a main stack of code that has priority. It runs first. When something that is asynchronous is encountered, it gets set aside, off the main stack, to be run later. That is just the way that JavaScript works.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  6 лет назад

      For more info, watch this great video - ruclips.net/video/8aGhZQkoFbQ/видео.html

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

    very very good Steve , thank you

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

    well explained. thanks.

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

    Awesome explanation

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

    great video on this matterr , very helpful

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

    Very much thanks bruh ,got clarity

  • @hakimimaula3512
    @hakimimaula3512 5 лет назад +4

    Sir, you deserve more views and likes, here, take mine, and hope you live well!!

  • @용원-h9b
    @용원-h9b 5 лет назад

    It helped me a lot thanks bro:)

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

    Thank you very much. Now i really understood promise

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

    The subtitle is disturbing mayn,

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

    It was very helpful Thank you so much.... :)

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

    Thanks a lot! I finally understand..

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

    Excelente video, muchas gracias; seria bueno que hablaras un poco mas despacio para los que no somos agiles en el ingles.

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

    awesome .... as usual

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

    The explanation is interesting. But the sound is a way too low to catch.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 года назад +1

      Yeah. This was just shortly before I got my good microphone. All the newer videos have much better audio.

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

    Great and clear.

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

    it helped. Thanks

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

    Thank you

  • @baxiry.
    @baxiry. 7 лет назад

    thank you very much

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

    very helpful!

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

    thank you!

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

    this is gold

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

    Alhamdulillah

  • @naumce.ivanovski
    @naumce.ivanovski 6 лет назад

    Thanks, man

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

    very effective!!

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

    Perfect!

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

    Awesome

  • @Eltopshottah
    @Eltopshottah 2 года назад +1

    🤯

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

    Awesome :)

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

    atleast increase the font sizes and cursor to a reasonable size while recording.

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

    confusing

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

    bull eye!

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

    Read more
    .
    .
    .
    .
    .
    .
    Show less