The async function is one of the newer additions to JavaScript.
Its purpose is to make asynchronous easier to read. It does this by making asynchronous code look synchronous.
What does that mean?
You’ll see it in this lesson 🙂.
Async and Await
You can declare a function to be an asynchronous function with the async keyword.
async function functionName(arguments) { // Do something asynchronous}In this function, you can use the await keyword.
This await keyword waits for a promise to be fulfilled.
So the following two sets of code are equivalent:
// Using a promisefunction getResource() { zlFetch('https://link-to-resource').then(response => { // Do something with response })}// Using Async functionsasync function getResource() { const response = await zlFetch('https://link-to-resource') // Do something with response}Catching errors
The await keyword always waits for a promise to be resolved. It cannot wait for a promise to be rejected.
If you want to handle errors in an async function, you need to use a try/catch block.
// Using Async functionsasync function getResource() { try { const response = await zlFetch('https://link-to-resource') // Do something with response } catch (e) { // Handle error }}Unfortunately, adding a try/catch makes code much harder to read. So instead of using try/catch, many professionals choose one of these two methods:
- Use a promise instead
- Catch the error after the async function rejects
Method 1: Using a promise instead
If you want to catch an error, it may make sense to use a promise directly.
// Using a promisefunction getResource() { zlFetch('https://link-to-resource') .then(response => { /* Do something with response */ }) .catch(error => { /* Handle Error */ })}If you compare this with the code above, you’ll see how a Promise can be easier to read when errors are involved.
Method 2: Catching the error after the async function rejects
One unique quirk about async functions is they are promises.
So you can handle the error with a catch method after running the async function. This may not always be desirable or doable though!
async function getResource() { const response = await zlFetch('https://link-to-resource') // Do something with response}
getResource().catch(error => { /* Handle Error */})Top-level await
Normally, await can only be used in an async function.
There is only one exception — when the environment supports top-level await.
This means:
- The environment creates an
asyncfunction wrapper around your context. - This allows you to use
awaitdirectly without using anasyncfunction.
But this also means you have to make sure you await a promise that does not fail — because any promise rejection will throw an unrecoverable error.
One example of an environment that supports top-level await is the module scope. You can access this by setting your script’s type to module.
<script src="main.js" type="module"></script>// Top-level awaitconst response = await fetch('link-to-resource')const data = await response.json()You can only set type to module if you serve your HTML files with a server. (You can’t use the file:// protocol).
If you don’t understand what the file:// protocol is, don’t worry. You’ll begin to understand in a few chapters.
Wrapping Up
That’s all you need to know about async functions!
They’re pretty easy to understand once you know they’re just promises underneath the hood.
So you can use async and await if you want to make your code look more synchronous. Or, you can use Promises if you prefer them.
Both methods work — so feel free to use either!
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson