Async-Await
Learn about asynchronous programming and leverage promises in JavaScript.
StartKey Concepts
Review core concepts you need to learn to master this subject
Resolving JavaScript Promises
Asynchronous JavaScript function
Async Await Promises
Using async await syntax
JavaScript async…await advantage
Async Function Error Handling
JavaScript aysnc await operator
Resolving JavaScript Promises
Resolving JavaScript Promises
let promise1 = Promise.resolve(5);
let promise2 = 44;
let promise3 = new Promise(function(resolve, reject) {
setTimeout(resolve, 100, 'foo');
});
Promise.all([promise1, promise2, promise3]).then(function(values) {
console.log(values);
});
// expected output: Array [5, 44, "foo"]
When using JavaScript async...await
, multiple asynchronous operations can run concurrently. If the resolved value is required for each promise initiated, Promise.all()
can be used to retrieve the resolved value, avoiding unnecessary blocking.
Async Await
Lesson 1 of 1
- 1Often in web development, we need to handle asynchronous actions— actions we can wait on while moving on to other tasks. We make requests to networks, databases, or any number of similar oper…
- 2The async keyword is used to write functions that handle asynchronous actions. We wrap our asynchronous logic inside a function prepended with the async keyword. Then, we invoke that function. asy…
- 3In the last exercise, we covered the async keyword. By itself, it doesn’t do much; async functions are almost always used with the additional keyword await inside the function body. The await key…
- 4We’ve seen that the await keyword halts the execution of an async function until a promise is no longer pending. Don’t forget the await keyword! It may seem obvious, but this can be a tricky mistak…
- 5The true beauty of async…await is when we have a series of asynchronous actions which depend on one another. For example, we may make a network request based on a query to a database. In that cas…
- 6When .catch() is used with a long promise chain, there is no indication of where in the chain the error was thrown. This can make debugging challenging. With async…await, we use try…catch sta…
- 7Remember that await halts the execution of our async function. This allows us to conveniently write synchronous-style code to handle dependent promises. But what if our async function contains mult…
- 8Another way to take advantage of concurrency when we have multiple promises which can be executed simultaneously is to await a Promise.all(). We can pass an array of promises as the argument to P…
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory