Sunday, May 12, 2024
114
rated 0 times [  119] [ 5]  / answers: 1 / hits: 7599  / 4 Years ago, sat, august 15, 2020, 12:00:00
const getNumberOfQuestions = async () => {
await this.channel.send('How many questions should I ask? (1-10)')
.then(async message => {
await this.channel.awaitMessages(message => message.author.id === this.owner && !isNaN(parseInt(message.content)), { max: 1, time: 15000 })
.then(collected => {
this.channel.send(`You asked for ${collected.first().content} questions.`);
return parseInt(collected.first().content);
})
.catch(collected => {
this.channel.send('You did not tell me how many questions you wanted. Ending the quiz.');
});
});
};

const getDifficulty = async () => {
await this.channel.send('What difficulty would you like: easy, medium, hard?')
.then(message => {
this.channel.awaitMessages(message => message.author.id === this.owner && ['easy', 'medium', 'hard'].includes(message.content.toLocaleLowerCase()), { max: 1, time: 15000 })
.then(collected => {
this.channel.send(`You asked for ${collected.first().content} difficulty.`);
return collected.first().content;
})
.catch(collected => {
this.channel.send('You did not tell which difficulty you wanted. Ending the quiz.');
});
});

};
getNumberOfQuestions();
getDifficulty();

With the above code, I do not want the execution to continue past this function when it is called. I clearly do not understand promises and await can some one help me?


.send and .awaitMessages both return a promise


More From » asynchronous

 Answers
7

First let me refactor your two procedures that have a mix of promises and async/await so that they have just equivalent async/await.


const getNumberOfQuestions = async () => {
const message = await this.channel.send('How many questions should I ask? (1-10)');
try {
const collected = await this.channel.awaitMessages(message => message.author.id === this.owner && !isNaN(parseInt(message.content)), { max: 1, time: 15000 });
this.channel.send(`You asked for ${collected.first().content} questions.`);
return parseInt(collected.first().content);
} catch(collected) {
this.channel.send('You did not tell me how many questions you wanted. Ending the quiz.');
}
};

const getDifficulty = async () => {
const message = await this.channel.send('What difficulty would you like: easy, medium, hard?');
try {
const collected = await this.channel.awaitMessages(message => message.author.id === this.owner && ['easy', 'medium', 'hard'].includes(message.content.toLocaleLowerCase()), { max: 1, time: 15000 });
this.channel.send(`You asked for ${collected.first().content} difficulty.`);
return collected.first().content;
} catch(collected) {
this.channel.send('You did not tell which difficulty you wanted. Ending the quiz.');
}
};

As you can see, awaiting on a promise is like treating what's after it as inside a then, where the resolved value is what's returned by the awaited expression. The rejection (.catch()) of the promise is translated to an exception which can be try{...}catch{...}ed.


Knowing this, what you're doing by calling


getNumberOfQuestions();
getDifficulty();

is calling both functions asynchronously - without waiting for the promises returned to settle. What you may want to do instead is to await the first's end before invoking the second. Like so:


await getNumberOfQuestions();
await getDifficulty();

However, the await keyword makes sense only in an async function, so you can do it like so:


(async()=>{
await getNumberOfQuestions();
await getDifficulty();
})();

More about async functions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function


[#2885] Wednesday, August 12, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lucianom

Total Points: 601
Total Questions: 98
Total Answers: 109

Location: Kenya
Member since Fri, Dec 23, 2022
1 Year ago
lucianom questions
Tue, Feb 22, 22, 00:00, 2 Years ago
Wed, May 5, 21, 00:00, 3 Years ago
Sun, Jan 24, 21, 00:00, 3 Years ago
Mon, Jun 22, 20, 00:00, 4 Years ago
Tue, Feb 18, 20, 00:00, 4 Years ago
;