JS Async/Await
In this page:
async function functionName() {
try {
const result = await promise;
} catch (error) {
// handle error
}
}
The async Keyword
Placing async before a function declaration marks it as asynchronous, allowing the use of await inside it, and automatically making the function itself return a promise, even if it looks like it returns a plain value.
उदाहरण: The async Keyword
async function getValue() {
return 42;
}
getValue().then(v => console.log(v)); // async function returns a promise
The await Keyword
await pauses an async function's execution at that exact line until the promise it's waiting on settles, then resumes with the resolved value, letting asynchronous code read in a natural, sequential order.
उदाहरण: The await Keyword
// Define the asynchronous function `run` with no parameters
// Define the asynchronous function `run` with no parameters
async function run() {
// Declare the constant `value`, set to `await Promise.resolve(10)`
// Declare the constant `value`, set to `await Promise.resolve(10)`
const value = await Promise.resolve(10);
// Print `value` to the console
// Print `value` to the console
console.log(value);
}
// Call `run()`
// Call `run()`
run();
Error Handling With try/catch
Wrapping await statements in a try block lets you catch a rejected promise's error in the matching catch block, providing a clean, familiar way to handle asynchronous errors without chaining .catch().
उदाहरण: Error Handling With try/catch
// Define the asynchronous function `run` with no parameters
// Define the asynchronous function `run` with no parameters
async function run() {
// Try running this block; jump to `catch` if it throws
// Try running this block; jump to `catch` if it throws
try {
// Wait for `Promise.reject("failed")` to resolve before continuing
// Wait for `Promise.reject("failed")` to resolve before continuing
await Promise.reject("failed");
// Catch any error, bound to `err`
// Catch any error, bound to `err`
} catch (err) {
// Print `"Caught:", err` to the console
// Print `"Caught:", err` to the console
console.log("Caught:", err);
}
}
// Call `run()`
// Call `run()`
run();
async/await vs Promises
async/await is built directly on top of promises, it's essentially a more readable syntax for the same underlying mechanism, which is why the two approaches can be mixed freely, and why understanding promises first makes async/await easier to grasp.
उदाहरण: async/await vs Promises
// Same result, two styles:
Promise.resolve(5).then(v => console.log(v));
async function run() {
const v = await Promise.resolve(5);
console.log(v);
}
run();
A Practical Example
Combining async/await with try/catch and multiple sequential steps shows the real strength of this pattern, complex asynchronous logic reads almost like straightforward, synchronous code.
उदाहरण: A Practical Example
// Define the asynchronous function `getUser` with no parameters
// Define the asynchronous function `getUser` with no parameters
async function getUser() {
// Try running this block; jump to `catch` if it throws
// Try running this block; jump to `catch` if it throws
try {
// Declare the constant `id`, set to `await Promise.resolve(1)`
// Declare the constant `id`, set to `await Promise.resolve(1)`
const id = await Promise.resolve(1);
// Declare the constant `name`, set to `await Promise.resolve("Sam")`
// Declare the constant `name`, set to `await Promise.resolve("Sam")`
const name = await Promise.resolve("Sam");
// Print `id, name` to the console
// Print `id, name` to the console
console.log(id, name);
// Catch any error, bound to `err`
// Catch any error, bound to `err`
} catch (err) {
// Print `"Error:", err` to the console
// Print `"Error:", err` to the console
console.log("Error:", err);
}
}
// Call `getUser()`
// Call `getUser()`
getUser();
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic