Understanding async/await
Section titled Understanding async/awaitIf you aren’t very familiar with ECMAScript 2017, you may not know about async/await. It’s a useful way to handle Promises in a hoisted manner. It’s also slightly faster and increases overall readability.
How do Promises work?
Section titled How do Promises work?Before we can get into async/await, you should know what Promises are and how they work because async/await is just a way to handle Promises. If you know what Promises are and how to deal with them, you can skip this part.
Promises are a way to handle asynchronous tasks in JavaScript; they are the newer alternative to callbacks. A Promise has many similarities to a progress bar; they represent an unfinished and ongoing process. An excellent example of this is a request to a server (e.g., discord.js sends requests to Discord’s API).
A Promise can have three states; pending, resolved, and rejected
The pending state means that the Promise still is ongoing and neither resolved nor rejected. The resolved state means that the Promise is done and executed without any errors. The rejected state means that the Promise encountered an error and could not execute correctly.
One important thing to know is that a Promise can only have one state simultaneously; it can never be pending and resolved, rejected and resolved, or pending and rejected. You may be asking, “How would that look in code?“. Here is a small example:
This example uses ES6 code. If you do not know what that is, you should read up on that here.
_16function deleteMessages(amount) {_16return new Promise((resolve) => {_16if (amount > 10) throw new Error("You can't delete more than 10 Messages at a time.");_16setTimeout(() => resolve('Deleted 10 messages.'), 2000);_16});_16}_16_16deleteMessages(5)_16.then((value) => {_16// `deleteMessages` is complete and has not encountered any errors_16// the resolved value will be the string "Deleted 10 messages"_16})_16.catch((error) => {_16// `deleteMessages` encountered an error_16// the error will be an Error Object_16});
_16function deleteMessages(amount) {_16return new Promise((resolve) => {_16if (amount > 10) throw new Error("You can't delete more than 10 Messages at a time.");_16setTimeout(() => resolve('Deleted 10 messages.'), 2000);_16});_16}_16_16deleteMessages(5)_16.then((value) => {_16// `deleteMessages` is complete and has not encountered any errors_16// the resolved value will be the string "Deleted 10 messages"_16})_16.catch((error) => {_16// `deleteMessages` encountered an error_16// the error will be an Error Object_16});
In this scenario, the deleteMessages
function returns a Promise. The .then()
method will trigger if the Promise resolves, and the .catch()
method if the Promise rejects. In the deleteMessages
function, the Promise is resolved after 2 seconds with the string “Deleted 10 messages.”, so the .catch()
method will never be executed. You can also pass the .catch()
function as the second parameter of .then()
.
How to implement async/await
Section titled How to implement async/awaitTheory
Section titled TheoryThe following information is essential to know before working with async/await. You can only use the await
keyword inside a function declared as async
(you put the async
keyword before the function
keyword or before the parameters when using a callback function).
A simple example would be:
_3async function declaredAsAsync() {_3// ..._3}
_3async function declaredAsAsync() {_3// ..._3}
or
_3const declaredAsAsync = async () => {_3// ..._3};
_3const declaredAsAsync = async () => {_3// ..._3};
You can use that as well if you use the arrow function as an event listener.
_3client.on('event', async (first, last) => {_3// ..._3});
_3client.on('event', async (first, last) => {_3// ..._3});
An important thing to know is that a function declared as async
will always return a Promise. In addition to this, if you return something, the Promise will resolve with that value, and if you throw an error, it will reject the Promise with that error.
Execution with discord.js code
Section titled Execution with discord.js codeNow that you know how Promises work and what they are used for, let’s look at an example that handles multiple Promises. Let’s say you want to react with letters (regional indicators) in a specific order. For this example, here’s a basic template for a discord.js bot with some ES6 adjustments.
_17const { Client, GatewayIntentBits } = require('discord.js');_17_17const client = new Client({ intents: [GatewayIntentBits.Guilds] });_17_17client.once('ready', () => {_17console.log('I am ready!');_17});_17_17client.on('interactionCreate', (interaction) => {_17if (!interaction.isChatInputCommand()) return;_17_17if (interaction.commandName === 'react') {_17// ..._17}_17});_17_17client.login('your-token-goes-here');
_17const { Client, GatewayIntentBits } = require('discord.js');_17_17const client = new Client({ intents: [GatewayIntentBits.Guilds] });_17_17client.once('ready', () => {_17console.log('I am ready!');_17});_17_17client.on('interactionCreate', (interaction) => {_17if (!interaction.isChatInputCommand()) return;_17_17if (interaction.commandName === 'react') {_17// ..._17}_17});_17_17client.login('your-token-goes-here');
If you don’t know how Node.js asynchronous execution works, you would probably try something like this:
_9client.on('interactionCreate', (interaction) => {_9// ..._9if (commandName === 'react') {_9const message = interaction.reply({ content: 'Reacting!', fetchReply: true });_9message.react('🇦');_9message.react('🇧');_9message.react('🇨');_9}_9});
_9client.on('interactionCreate', (interaction) => {_9// ..._9if (commandName === 'react') {_9const message = interaction.reply({ content: 'Reacting!', fetchReply: true });_9message.react('🇦');_9message.react('🇧');_9message.react('🇨');_9}_9});
But since all of these methods are started at the same time, it would just be a race to which server request finished first, so there would be no guarantee that it would react at all (if the message isn’t fetched) or in the order you wanted it to. In order to make sure it reacts after the message is sent and in order (a, b, c), you’d need to use the .then()
callback from the Promises that these methods return. The code would look like this:
_14client.on('interactionCreate', (interaction) => {_14// ..._14if (commandName === 'react') {_14interaction.reply({ content: 'Reacting!', fetchReply: true }).then((message) => {_14message_14.react('🇦')_14.then(() => message.react('🇧'))_14.then(() => message.react('🇨'))_14.catch((error) => {_14// handle failure of any Promise rejection inside here_14});_14});_14}_14});
_14client.on('interactionCreate', (interaction) => {_14// ..._14if (commandName === 'react') {_14interaction.reply({ content: 'Reacting!', fetchReply: true }).then((message) => {_14message_14.react('🇦')_14.then(() => message.react('🇧'))_14.then(() => message.react('🇨'))_14.catch((error) => {_14// handle failure of any Promise rejection inside here_14});_14});_14}_14});
In this piece of code, the Promises are chain resolved with each other, and if one of the Promises gets rejected, the function passed to .catch()
gets called. Here’s the same code but with async/await:
_9client.on('interactionCreate', async (interaction) => {_9// ..._9if (commandName === 'react') {_9const message = await interaction.reply({ content: 'Reacting!', fetchReply: true });_9await message.react('🇦');_9await message.react('🇧');_9await message.react('🇨');_9}_9});
_9client.on('interactionCreate', async (interaction) => {_9// ..._9if (commandName === 'react') {_9const message = await interaction.reply({ content: 'Reacting!', fetchReply: true });_9await message.react('🇦');_9await message.react('🇧');_9await message.react('🇨');_9}_9});
It’s mostly the same code, but how would you catch Promise rejections now since .catch()
isn’t there anymore? That is also a useful feature with async/await; the error will be thrown if you await it so that you can wrap the awaited Promises inside a try/catch, and you’re good to go.
_12client.on('interactionCreate', async (interaction) => {_12if (commandName === 'react') {_12try {_12const message = await interaction.reply({ content: 'Reacting!', fetchReply: true });_12await message.react('🇦');_12await message.react('🇧');_12await message.react('🇨');_12} catch (error) {_12// handle failure of any Promise rejection inside here_12}_12}_12});
_12client.on('interactionCreate', async (interaction) => {_12if (commandName === 'react') {_12try {_12const message = await interaction.reply({ content: 'Reacting!', fetchReply: true });_12await message.react('🇦');_12await message.react('🇧');_12await message.react('🇨');_12} catch (error) {_12// handle failure of any Promise rejection inside here_12}_12}_12});
This code looks clean and is also easy to read.
So you may be asking, “How would I get the value the Promise resolved with?“.
Let’s look at an example where you want to delete a sent reply.
_11client.on('interactionCreate', (interaction) => {_11// ..._11if (commandName === 'delete') {_11interaction_11.reply({ content: 'This message will be deleted.', fetchReply: true })_11.then((replyMessage) => setTimeout(() => replyMessage.delete(), 10000))_11.catch((error) => {_11// handle error_11});_11}_11});
_11client.on('interactionCreate', (interaction) => {_11// ..._11if (commandName === 'delete') {_11interaction_11.reply({ content: 'This message will be deleted.', fetchReply: true })_11.then((replyMessage) => setTimeout(() => replyMessage.delete(), 10000))_11.catch((error) => {_11// handle error_11});_11}_11});
The return value of a .reply()
with the fetchReply
option set to true
is a Promise which resolves with the reply when it has been sent, but how would the same code with async/await look?
_10client.on('interactionCreate', async (interaction) => {_10if (commandName === 'delete') {_10try {_10const replyMessage = await interaction.reply({ content: 'This message will be deleted.', fetchReply: true });_10setTimeout(() => replyMessage.delete(), 10000);_10} catch (error) {_10// handle error_10}_10}_10});
_10client.on('interactionCreate', async (interaction) => {_10if (commandName === 'delete') {_10try {_10const replyMessage = await interaction.reply({ content: 'This message will be deleted.', fetchReply: true });_10setTimeout(() => replyMessage.delete(), 10000);_10} catch (error) {_10// handle error_10}_10}_10});
With async/await, you can assign the awaited function to a variable representing the returned value. Now you know how you use async/await.