JavaScript Async Await




One of the hardest things about writing good JavaScript is dealing with heavily nested asynchronous code. Promises were created to solve the problem with callback hell, but there are still plenty of nested problems related to promises. This is where async/await comes in. JavaScript added async/await to allows developers to write asynchronous code in a way that looks and feels synchronous. This helps to remove many of the problems with nesting that promises have, and as a bonus can make asynchronous code much easier to read and write. In this video I am going to explain what async/await is and how to use async/await to rewrite your existing promise based code.

Learn Promises First:

Twitter:

GitHub:
https://github.com/WebDevSimplified

CodePen:
https://codepen.io/WebDevSimplified

#JavaScript #AsyncAwait #Promises

Original source


32 responses to “JavaScript Async Await”

  1. So if I get it right.. Anything after the line of "await" keyword will only run when the promise has been resolved?
    Like
    await fetchSomething()
    // Will only run when the fetchSomething Promise has been resolved

  2. nice usage of async await is create a "wait/pause" function with setTimeout

    function wait(time) {
    return new Promise(res => setTimeout(res,time));
    }

    async myMainFunction() {
    //do something

    //wait 1 second
    await wait(1000);

    //do something else
    }

  3. this is absolutely amaze.
    my friend actually works at google and he always watches your tutorials and he told me to do that too.
    I have an interview with amazon very soon.
    I will try to refactor some of my projects that I built using promises since async await is way more clean than promise.
    thanks very much brother

  4. Great video, but I am a bit confused because around 3:45 you say the await keyword makes the async function to wait for the makeRequest to finish, but then you say around 4:10 the async function won't wait for the await function and keep on going. If you could clarify it would be helpful. Thanks

  5. 32 people still loving PHP .. jaja
    Thanks so much for your videos, I really love them, you are one of my best favorite channels on youtube. I usually never comment, but this time I had to do it. greetings from Costa Rica

  6. I have watched few videos on async/await over YouTube, but couldn't able to understand this concept. Then I have realized that I should make my foundation strong first on promise. Now I can clearly see how it's working under the hood. Anyway, nice explanation Kyle

Leave a Reply