Promise vs Async/Await
People think async and await replace promises for good, and they do not. An async function still hands back a promise. The word await is a tidier way to wait for one, so the code reads from top to bottom. Under the hood it is the same promise. The then and catch calls just turn into plain lines inside a try and catch.
Promise (JavaScript)
An object for a result not ready yet. It ends in a value or an error.
A promise is an object that stands for a result that is not ready yet. It starts as pending, then ends as fulfilled with a value or rejected with an error. You attach what to do next with then, and what to do on failure with catch. Promises can be chained, so steps read in order instead of nesting. Other languages call the same idea a future.
Async/Await
Async code that reads top to bottom. await waits for each result.
Async/await lets you write async code that reads like normal step-by-step code. You mark a function with async, then put await in front of a slow call. At an await, the function pauses until the result is ready, while the rest of the program keeps running. Errors are caught with a plain try and catch. JavaScript, Python, C# and Rust all have it.
In one line
- Promise (JavaScript)
- An object for a result not ready yet. It ends in a value or an error.
- Async/Await
- Async code that reads top to bottom. await waits for each result.