Promises are one of the best additions to JavaScript because they make managing asynchronous code a lot easier. Switching from callbacks to promises sounds like a big upgrade, but there's something even better than promises and that's async/await. Async/wait is an alternative syntax to promises that makes reading/writing asynchronous code easier, but there are a …
Promises are one of the best additions to JavaScript because they make managing asynchronous code a lot easier. Switching from callbacks to promises sounds like a big upgrade, but there’s something even better than promises and that’s async/await. Async/wait is an alternative syntax to promises that makes reading/writing asynchronous code easier, but there are a few caveats you should know about async/wait, otherwise you may end up messing up your code his got worse.
Asynchronous/pending database
To understand async/await, the easiest way is to start with an example of promises used and then convert it to async/await. To get started, we’ll be using the function below in all of our examples.
function setTimeoutPromise(delay) { return new Promise((determination, pride) => {if (delay < 0 xss=removed> {resolve(‘You waited for ${delay} milliseconds’)}, delay)}) }
This function is simply a promise-based version of setTimeout. Now let’s see how to chain two timeouts where the second timeout is waiting for the first timeout to finish.
setTimeoutPromise(250).then(msg => {console.log(msg)console.log(“First timeout”)return setTimeoutPromise(500) }).then(msg => {console.log(msg) console.log( “Monday timeout”) }) // Output:
// You waited for 250 milliseconds // First timeout // You waited 500 milliseconds // Second timeout
If you’re used to promises, this code shouldn’t be too confusing. The most confusing part of the code is that we are returning the second promise from the first so we can chain them together. Now this code works fine, but we can make it much cleaner with async/await.
asynchronous function doStuff() doStuff() {const msg1 = wait setTimeoutPromise(250)console.log(msg1)console.log(“First timeout”)const msg2 = wait setTimeoutPromise(500)console.log( msg2)console. log(“Second timeout”) } // Output:
// You waited for 250 milliseconds // First timeout // You waited 500 milliseconds // Second timeout
The code above does exactly the same as the previous version, but you’ll notice it looks more like regular synchronous code, which is the point of async/await. Now let’s talk about how this code works.
First, you’ll notice that we’ve wrapped all of our code in a function called doStuff. The name of the function doesn’t matter, but you’ll notice that we’ve labeled this function asynchronous by placing the async keyword before the function keyword. This tells JavaScript that we intend to use the await keyword in this function. If we don’t label the function as asynchronous and use the await keyword in the function, an error will occur.
Also note that from now on you cannot use the await keyword unless you are inside a function, so we have to create a function to run this code. This is something JavaScript plans to change by adding a top-level await, which means you can use a file’s top-level wait without being inside a function, but this still won’t happen. in all browsers.
Now that we understand the async keyword, let’s talk about the code inside the function. You’ll notice it looks a lot like the previous code, and that’s because async/await is just another way to write the same stuff. To convert a .then promise to async/await, you need to call the promise function setTimeoutPromise(250) and put the await keyword before it. This tells JavaScript that the function after the wait keyword is asynchronous. Then, if your promise returns a value, you can simply access that value as if it were a normal return function, just like we did with const msg1 = await setTimeoutPromise(250) . The final step is to take all the code from the .then part of the promise and put it after the function call.





