FAQ: Async Await - The await Operator

The code is correct it’s just Codecademy’s bash problem.

I am extremely confused with Promises but from what I learned for last few weeks it is clear to me that this is “.then(argument)” element that controls asynchronization, not “async” or “await” or etc…
I removed “await” from final code and it still worked the same way.
I changed “.then” argument into neutral “.then(console.log)” and suddenly it stopped asynchronising.
It is the same for Promise or fetch - .then() with “resolve” argument is all we need.

Would you mind sharing the code?

.then() is used when the argument has been resolved, and you’ll get the resolved value. .catch() is used when the argument has been rejected, and you will get the rejected value.

There is no real difference, they are the same thing. Using async…await does the same thing as using native promise syntax, it’s just that most people find async…await more readable because it reads like synchronous code

Is there a reason to use let instead of const for the variable that stores the await function? as the example shows:

async function announceDinner() {

  // Write your code below:

  let meal = await brainstormDinner();

  console.log(`I'm going to make ${meal} for dinner.`);

}

I would think, since we are not interested in re assigning this variable const would make more sense.

const would be more appropriate. If the data in the variable will change later on then use let.
If the data is constant which is in science we have something called constant variable meaning something will not change, same to const. const is used when the data will not be changed later on, the data will stay the same you can’t change it later on.

Yes, while you can’t reassign a const, the value inside can change. For example, in the code snippet posted above the you could modify the brainstormDinner(), then call again meal and would give you a different value.

The question I was asking is: why use let on this function? Wouldn’t const be a better practice to avoid mistakenly re assigns meal and break the code, or worse, code still runs but doesn’t work as expected.

Using let this would be possible:

async function announceDinner() {

  // Write your code below:

  let meal = await brainstormDinner();
  meal = 'soup';

  console.log(`I'm going to make ${meal} for dinner.`);

}

Using const, it would throw an error.

in 3/9 how was meal defined as ‘beans’?
in library.js there was resolve(‘beans’)
but I don’t see how it defines meal

In 5/9 we define beanType within ShopforBeans with setTimeout()

setTimeout(() => {
let beanType = [beanTypes.randomIndex]

and we use this variable soakTheBeans(beanType)
How is it possible to use beanType here if it was only defined within the local scope of shopForBeans?

// Native promise version:

function nativePromiseDinner() {

  brainstormDinner().then((meal) => {

    console.log(`I'm going to make ${meal} for dinner.`);

  });

Don’t we need to add a missing return keyword in the function above?

// Native promise version:

function nativePromiseDinner() {

  return brainstormDinner().then((meal) => {

    console.log(`I'm going to make ${meal} for dinner.`);

  });

I think something is amiss here in this lesson. I made sure the code was correct by clicking on “Check Work” before running it. Has anyone else had this problem? This is actually the second time it’s happened within this lesson. Below is what I get in the console when I execute the code:
$ node app.js
shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
path.js:1142
cwd = process.cwd();
^

Error: ENOENT: no such file or directory, uv_cwd
at Object.resolve (path.js:1142:25)
at startup (bootstrap_node.js:129:32)
at bootstrap_node.js:542:3
$

I have only one question: how does this function trigger console.log from the brainstormDinner() function?
Our meal variable waits until the Promise state changes to fulfilled and gets beans value. But why messages are displayed to the console while waiting is not clear to me.

// Promise function
const brainstormDinner = () => {
  return new Promise((resolve, reject) => {
  console.log(`I have to decide what's for dinner...`)
  setTimeout(() => {
    console.log('Should I make salad...?');
    setTimeout(() => {
      console.log('Should I make ramen...?');
      setTimeout(() => {
        console.log('Should I make eggs...?');
        setTimeout(() => {
          console.log('Should I make chicken...?');
          resolve('beans');
        }, 1000);
      }, 1000);
    }, 1000);
  }, 1000);
});
};

// async function
async function announceDinner() {
  const meal = await brainstormDinner();
  console.log(`I'm going to make ${meal} for dinner.`);    
}
announceDinner();