FAQ: Async Await - Handling Dependent Promises

This community-built FAQ covers the “Handling Dependent Promises” 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 Dependent Promises

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!

Is there a situation where using native promise syntax would be better than using the async...await syntax?

3 Likes

is it possible to rewrite the promises in library.js as async functions?

i tried rewriting the last one “cookTheBeans()” but when i ran app.js that part of the code resulted as “undefined”

2 Likes

apparently deleting the setTimeOut function fixes it

i don’t know why though is some one can explain this to me that would help

Why is the argument of soakTheBeans() function “type”? I thought it would be “beanType”…

4 Likes

Hello @ifrancesalcantara.

Because of this:
image

type is the variable that gets the type of beans purchased assigned to it when we call the shopForBeans() function. We then pass type as the argument to soakTheBeans(type). In the soakTheBeans() function the type we passed to the function is assigned to the parameter beanType.

4 Likes

When should we use native promise syntax and when should we use async... wait?

When I “Check Work”, I get the green checkmarks but get an error in the bash window.
node:1164) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ReferenceError: soakTheBeans is not defined
There is no soakTheBeans() or cookTheBeans() functions in library.js.

My output to the bash window before the error prints line 2. twice:

  1. Heading to the store to buy beans…
  2. I bought garbanzo beans because they were on sale.
  3. I bought black beans because they were on sale.
  4. Great! I’m making garbanzo beans for dinner tonight!

I’m quite confused as to what is happening.

since we are not asked to amend or change the code in function library, your output pretty much comes from the app.js where you do the exercise, so it’s better if you can copy your code from app.js and paste it here then others can assist better.

Why does soakTheBeans expect ‘type’ as it’s expected value? Shouldn’t it be ‘beanType’ as that’s what was given in library.js?

let isSoft = await soakTheBeans(type);

Also, where is the ‘dinner’ variable coming from?

console.log(dinner);

Edit: I’m a dummy. Just noticed where all of this was coming from. Nevermind ya’ll

Question: How do we amend the cookTheBeans function so as to log the beanType in the console?

let cookTheBeans = (isSoftened) => {
  return new Promise((resolve, reject) => {
    console.log('Time to cook the beans.');
    setTimeout(()=>{
      if (isSoftened) {
        console.log('... The ${beanType} beans are cooked!');
        resolve('\n\nDinner is served!');
      }
    }, 1000);
  });
}

We could do it this way.

First we edit cookTheBeans() in library.js

let cookTheBeans = (isSoftened, beanType) => {
  return new Promise((resolve, reject) => {
    console.log('Time to cook the beans.');
    setTimeout(()=>{
      if (isSoftened) {
        console.log(`... The ${beanType} beans are cooked!`);
        resolve('\n\nDinner is served!');
      }
    }, 1000);
  });
}

Don’t forget to use back ticks to use a template literal so that your ${beanType} expression will actually print the bean type instead of “${beanType}”

Now, let’s edit our makeBeans() function to look like this:

  let type = await shopForBeans();
  let isSoft = await soakTheBeans(type);
  let dinner = await cookTheBeans(isSoft, type);
  console.log(dinner);
};

Hope this helps!

3 Likes

image

And then we have my code…

async function makeBeans() {
 
}

I don’t understand why I can’t progress. I even tried copy/pasting the “hint” and renaming the function and still can’t move on. Even copy/pasting the SOLUTION doesn’t work.

Copy/Pasting the code solution and running node app.js results in the following error:

$ node app.js
/home/ccuser/workspace/async-await-multiple-awaits/library.js:31
      if (isSoftened) {
      ^^
SyntaxError: Unexpected token if
    at createScript (vm.js:53:10)
    at Object.runInThisContext (vm.js:95:10)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/ccuser/workspace/async-await-multiple-awaits/app.js:1:114)

Could you show us the code in library.js?

in 5/9 in the end of the exercise I typed node app.js in the terminal, but I got nothing

Did you do step 7?

Beneath your function declaration, invoke the makeBeans() function.

If you didn’t add in the last line this instruction makeBeans(), the function will not be called and nothing will happen.

// anothe way to rewrite the code in (library.js) using async

const shopForBeans = async () => {

const beanTypes = [‘kidney’, ‘fava’, ‘pinto’, ‘black’, ‘garbanzo’];

let randomIndex = Math.floor(Math.random() * 5);

let beanType = beanTypes[randomIndex];

console.log(I bought ${beanType} beans because they were on sale.);

return beanType;

}

let soakTheBeans = async (beanType) => {

console.log(‘Time to soak the beans.’);

console.log(`... The ${beanType} beans are softened.`);

return true;

}

let cookTheBeans = async (isSoftened) => {

console.log(‘Time to cook the beans.’);

 if (isSoftened) {

  console.log('... The beans are cooked!');

  return '\n\nDinner is served!';

}

}

Why do we pass in the first promise as an argument when returning the second promise ?