JavaScript Callbacks, Promises, and Async Await - How to Return a Response from an Asynchronous call

Sdílet
Vložit
  • čas přidán 7. 07. 2024
  • In this video, we look at how to return a response from an asynchronous call in JavaScript. If you are trying to return a value from your asynchronous function in JavaScript and you are getting undefined as the value then this is the video that you must watch. It covers all the methods that you need to know in order to return a value from an asynchronous call in JavaScript. We compare JavaScript Callbacks vs JavaScript Promises vs Async Await in Javascript and see where to use which pattern to deal with Asynchronous requests in JavaScript.
    📖 Video Chapters
    00:00 Video Intro
    00:19 Difference between asynchronous and synchronous functions
    00:32 Understanding synchronous functions
    01:48 Understanding asynchronous functions
    03:26 Faking an asynchronous call
    04:35 Callback Functions Explained
    08:01 Promises in JavaScirpt
    11:57 Async Await In JavaScript

Komentáře • 6

  • @sushantbhargav4652
    @sushantbhargav4652 Před 2 lety

    why and how you put add(a:1,b:2) notation, why it is used ? ( a:1 notation)

    • @cloudaffle
      @cloudaffle  Před 2 lety

      Sushant that's just my IDE reminding me that there are 2 arguments that I need to provide these arguments to this function nothing really specific that I'm using here it's vanilla JavaScript

    • @leabaty7133
      @leabaty7133 Před 2 lety

      @@cloudaffle is that an extension ? That's very useful indeed :)

  • @darrellleong4780
    @darrellleong4780 Před rokem +1

    But how can we return the sum outside of the async function? We can console.log the desired result here, but it's useless if we can't assign it to a global variable.

  • @The.SpiritualScientist

    Thanks a lot for uploading this video. It made a lot of concepts clear in one go. Just wanted to ask if this approach can be used to export values from a module to another? E.g. If rather console logging, I want to export the value of the variable 'sum' from this module to another and print it there, will this approach still work?

    • @cloudaffle
      @cloudaffle  Před rokem

      I'm glad I was able to help. When you export a promise you would need to resolve it before you can use the value as the execution is asynchronous. You can use an async function export it and use the try and catch block to await for the async execution to complete. So in short you can treat an async function like synchronous code and export resulting values till the resulting promise is not resolved