Difference Between Async and Await: The Ultimate Guide

Bobby Jul 13, 2026

Async and await are two fundamental keywords in modern JavaScript for enabling asynchronous operations. MongoDB, in its JavaScript driver, utilizes them to execute database operations asynchronously without blocking the event loop. Let's dive into the intricacies of async and await to understand how they work together.

Async/Await
Async/Await

Async functions, declared with the "async" keyword, enable the use of the "await" keyword within them. This pair allows us to write asynchronous code that looks and behaves like synchronous, sequential code. They are a powerful tool for handling promises and other asynchronous operations in a more straightforward and readable manner.

Async/Await Explained Simply
Async/Await Explained Simply

Understanding Async

Async functions are designed to always return a promise. Even if the function doesn't explicitly return a promise, JavaScript wraps the returned value in a promise. This ensures that the async function behaves predictably in asynchronous scenarios.

Mastering Salesforce Integration Patterns: A Technical Guide to Sync vs Async
Mastering Salesforce Integration Patterns: A Technical Guide to Sync vs Async

Async functions are primarily used to simplify the asynchronous workflows. They allow us to rearrange the code into a more linear, synchronous-like structure, making it easier to understand and maintain. Let's explore the mechanics of async functions with two essential subtopics: Promise Returns and Non-Promises in Async Functions.

Promise Returns

JavaScript|async, awaitを利用して非同期処理を制御 - わくわくBank
JavaScript|async, awaitを利用して非同期処理を制御 - わくわくBank

When an async function returns a value, that value is treated as the fulfillment value of the promise produced by the function. This means that the promise returned by an async function is resolved with the return value of the function.

For example, consider an async function that returns a simple integer:

```javascript async function test() { return 42; } test().then(console.log); // Logs 42 ```

Non-Promises in Async Functions

Asynchronous JavaScript: Promises, Async/Await, and Callbacks - Makemychance
Asynchronous JavaScript: Promises, Async/Await, and Callbacks - Makemychance

When an async function reaches a return statement with a non-promise value, that value is treated as the fulfillment value of the promise produced by the function, just like in the previous example. However, if the function returns a thenable (i.e., an object with a then method), then the promise produced by the async function is handled using the thenable's then method.

This is crucial for understanding how async functions interact with promises and other thenables. It's essential to consider this behavior when designing the control flow of your async functions.

Dissecting Await

Difference Between Synchronous and Asynchronous Counter
Difference Between Synchronous and Asynchronous Counter

Await is used to wait for a promise to resolve or reject. It can only be used inside an async function and will pause the execution of the async function and wait for the promise to complete.

Await accepts a promise as an argument and waits for its completion, then resumes the execution of the async function. If the promise is resolved, the value is passed to the next line. If it's rejected, the async function will throw an error, which can be caught using try/catch blocks.

The excessive usage of async-await
The excessive usage of async-await
Async and Await in JavaScript
Async and Await in JavaScript
Asynchronous Programming. Await the Future
Asynchronous Programming. Await the Future
Async Programming with Callbacks, Promises, and Async/Await -Part 1-
Async Programming with Callbacks, Promises, and Async/Await -Part 1-
the differences between sast and datts infographical poster by syopsys
the differences between sast and datts infographical poster by syopsys
an info sheet describing the differences between synconous and asynchronous
an info sheet describing the differences between synconous and asynchronous
JavaScript: Promises and Why Async/Await Wins the Battle
JavaScript: Promises and Why Async/Await Wins the Battle
Anymore vs. Any More – What’s the Difference?
Anymore vs. Any More – What’s the Difference?
Waiting vs Awaiting: Difference and Comparison
Waiting vs Awaiting: Difference and Comparison
Synchronous vs Asynchronous: Difference and Comparison
Synchronous vs Asynchronous: Difference and Comparison
two different types of net vs asp net comparison chart, with the same text
two different types of net vs asp net comparison chart, with the same text
How Does Angular Know When to Update the UI? | Damith Anushka
How Does Angular Know When to Update the UI? | Damith Anushka
Angular vs React vs Vue: Which One is The Best
Angular vs React vs Vue: Which One is The Best
How to use Async Await in JavaScript
How to use Async Await in JavaScript
Arithmetic vs Geometric Sequence: Difference and Comparison
Arithmetic vs Geometric Sequence: Difference and Comparison
the different types of computers are shown in this diagram, which shows how to use them
the different types of computers are shown in this diagram, which shows how to use them
Difference between Abstraction and Encapsulation
Difference between Abstraction and Encapsulation
Alternate vs Equivalent: Difference and Comparison
Alternate vs Equivalent: Difference and Comparison
What is Asynchronous Programming? Explained
What is Asynchronous Programming? Explained
Read “Finding the balance: speed vs quality“ by Dean Harrison on Medium: https://bootcamp.uxdesign.
Read “Finding the balance: speed vs quality“ by Dean Harrison on Medium: https://bootcamp.uxdesign.

Waiting on Promises

Await's primary purpose is to wait for a promise to complete before proceeding with the execution of the async function. It's important to note that await doesn't block the entire JavaScript execution; it only blocks the async function's execution until the promise is fulfilled. Other async functions can still run concurrently, making await a powerful tool for managing asynchronous workflows.

The following example demonstrates the usage of await with a promise: ```javascript console.log('Start'); async function test() { const promise = new Promise((resolve) => setTimeout(() => resolve('Resolved!'), 2000)); const result = await promise; console.log(result); } test(); console.log('End'); // Logs: // Start // End // Resolved! ```

Handling Errors with Await

When an error is thrown inside an async function, it's wrapped in a rejected promise. This allows us to use try/catch blocks to handle rejected promises, making error handling in async functions straightforward and familiar.

Here's an example of using try/catch to handle a rejected promise in an async function: ```javascript async function test() { try { const response = await fetch('https://nonexistent-domain.com'); console.log(response); } catch (error) { console.error('Error:', error); } } test(); // Logs: Error: [TypeError: Failed to fetch] ```

In conclusion, understanding the difference between async and await is vital for working with promises and other asynchronous operations in modern JavaScript. Async functions enable us to write asynchronous code that's easy to reason about, while await simplifies the handling of promises by allowing us to pause execution until a promise is fulfilled. Embracing these tools will improve your JavaScript development experience and result in more performant and maintainable applications.