FAQ: Async Await - Handling Errors

This community-built FAQ covers the “Handling Errors” exercise from the lesson “Async Await”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Asynchronous JavaScript

FAQs on the exercise Handling Errors

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

HELP!
I have completed the exercise and running ‘node app.js’ shows that the code works as expected. However when I hit the check my work button at any point after stage 1 the webpage for the lesson freezes and crashes. How can I fix this?

Code below:

const cookBeanSouffle = require('./library.js');

// Write your code below:
async function hostDinnerParty()
{
  
  try{
    
    let isCook = await cookBeanSouffle();
    console.log(isCook + ' is served!');
    
  } catch(error){
    
    console.log(error);
    console.log('Ordering a pizza!');
    
  }
  
};

hostDinnerParty();
4 Likes

I am having the same problems. I am using the code that Codecademy has provided and the browser still freezes.

I have tried it on Chrome, FireFox, and Opera. It seems to not freeze on Safari on Mac.

However, even if the answer is correct, the code is marked as incorrect by the site.

First post here but I had to reply as well because the issue is still present. The first time i got to step 3 but then it wouldn’t work so I reset the exercise. Consequently, it would continue to freeze at step 2 and crash so I ended up just skipping it by making it give the solution.

To the last part of tesla’s response, some questions are glitchy with accepting answers.
I had a bug earlier where my code was correct (in fact, the same as the solution) and it wouldn’t accept. I used the solution they gave which didn’t work either. I had to reset and refresh before it would finally take my answer.

4 Likes

Rather poor implementation of randomSuccess in library.js.

It is just bad practice to ever return true or false.

Please refactor from:
let randomSuccess = () => {
let num = Math.random();
if (num < .5 ){
return true;
} else {
return false;
}
};

to

let randomSuccess = () => {
return Math.random() < .5
};

2 Likes

yes its so confusing because they make it look like you need to write an array.

they made it look like you need to make an array or something which was very confusing. I ended up getting the solution also

1 Like

Hi,
it surely is a silly question but I wonder why this

async function hostDinnerParty() {
  try{
    let isServed = await cookBeanSouffle();
    console.log(isServed+' is served!');
  }
  catch (error) {
    console.log(error);
    console.log('Ordering a pizza!');
  }
}
hostDinnerParty();

logs either
“Bean Souffle is served!” or “Dinner is ruined!” instead of
“Bean Souffle is served!” or “Dinner is ruined! is served!”

Thanks and sorry for the stupid question, (I think I missed something as I didnt take the lessons in the syllabus order (due to something i’m not aware of, I always click “Start next lesson” at the end of them…?))

Hello, @apaep.

The function cookBeanSouffle() was provided for us in the library.js file:

const cookBeanSouffle = () => {
 return new Promise((resolve, reject) => {
   console.log('Fingers crossed... Putting the Bean Souffle in the oven');
   setTimeout(()=>{
     const success = randomSuccess();
     if(success){
       resolve('Bean Souffle');
     } else {
       reject('Dinner is ruined!');
     }
   }, 1000);
 })
};

When your code calls cookBeanSouffle() we await a promise.

From the lesson text in step 1:
image

So, either you get the resolved value 'Bean Souffle' which is assigned to isServed, and then printed to the console with ' is served!' concatenated to the end of it, or we get the rejected value 'Dinner is ruined!'. Since the promise was rejected. The try condition fails, and the rejected value is passed and assigned to the error parameter. The error is printed to the console followed by the 'Ordering a pizza!' message.

This topic started with a ‘freezing problem’. I had the same problem today (in FireFox, at work and at home). Kept on freezing evaluating the second step, while my solution was right. Thanks to this comment section I refreshed the questions and gave no answer in section 2. Then skipped to View Solution.
As I am an absolute beginner, this section of the course is (still) beyond my executive power. I still need a lot of basic beginner’s practice. However, I do understand all the concepts and code (yeah), and after finishing this course I will go for a second round!

3 Likes

This lesson goes into a infinite loop on chrome 80.0.3987.87 as of feb 5, 2020

:confused:

2 Likes

on a different topic but same question.
Question 3’s hint gives us:
try {
let dinner = await cookBeanSouffle();
console.log(${dinner} is served!);
}

Before looking at the hint, i wrote:
try{
console.log(${await cookBeanSouffle()} is served)
}catch(error){

}

How come we need to declare a variable and then assigned that variable on console.log? Presumable, declaring a variable is one step more to accomplish the end.
Please explain why this is so? And if both ways are true, why doesn’t Codeacademy lets us use both?
Thanks.

1 Like

That’s the accepted way of writing something that should be inserted in code, though. If you look at functions with optional parameters they might look like functionName(parameter1[, optional parameter]). They also clarified what it’s supposed to look like below in plain English, which I’m sure you can read.

1 Like

You actually don’t have to define a variable. Codecademy let me pass. Maybe you need to write an exclamation mark? I’m not sure how this lesson checks for valid solutions.

2 Likes

Hello.
I have a problem with this exercise. I am stuck at task 4 where it’s expected from me to fill catch block. I wrote it, checked hint and it is exactly like it, but it still is not good answer somehow? Please help.

const cookBeanSouffle = require('./library.js');

// Write your code below:

async function hostDinnerParty(){
try {
  let meal = await cookBeanSouffle();
console.log(`${meal} is served!`);
}
catch (error) {
  console.log(error);
  cosnole.log('Ordering a pizza!');

}

};

Thank you.

This is the best solution!

1 Like

You have a typo in the last console.log:
Your wrote coSNole.log instead of coNSole.log

**Freeze ? **

Clear the app.js, press check work until get the solution and skip the exercice

Thats not my idea. But with this post will be clear.
thanks to :
net2479449710

PS how ironic, error in error handling execice…

I like to try to write the complete solution (all the steps) before clicking “Check Work”. Usually that is fine. In this case, it wasn’t.

I could not get the second step to pass even with the following code:

async function hostDinnerParty() {
  try {
    // let result = await cookBeanSouffle();
    // console.log(`${result} is served!`);
  } catch (error) {
    // console.log(error);
    // console.log('Ordering a pizza!');
  }
}

It did, however, work without the comments:

async function hostDinnerParty() {
  try {

  } catch (error) {

  }
}

upd. sorry i forgot arrow function =) i’ll leave the comment just in case

Hello. Why this piece of code throws an error?

let hostDinnerParty= async () { 
  try {
    let dinner = await cookBeanSouffle();
    console.log(`${dinner} is served!`)
  }
  catch(error) {
    console.log(error);
    console.log('Ordering a pizza!')
  }
}

изображение
I thought we can use variables to store async...await functions. Is it different for try...catch?