Quote generator help please

I must be missing something important about using async / await. I do not understand why i can not store the quotes in a variable declared outside of the async function. i can access the data in the function but can not store it. Some one please point me in the right direction. I have looked at several examples but most I see send the response to another function. I tried passing it to another function just to store it in a variable , but still did not work. Anything outside of the async function just gives me undefined.

Your code so far

let quotes;

const getQuotes = async () => {
  let response = await fetch(
  if (response.ok) {
    //window.alert("Camperbot is in the office.");
    quotes = await response.json();
console.log(quotes); // undefined?

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0

Challenge: Build a Random Quote Machine

Link to the challenge:

I do not understand why i can not store the quotes in a variable declared outside of the async function.

Your understanding is not correct, your code is already accomplishing this.

getQuotes() is asynchronous, so the interpreter moves off to the next instruction immediately. In other words, quotes has not been defined yet by the time the interpreter reaches the log. If you moved the quotes log to the end of getQuotes, you can see that it’s working fine.

You could further wrap everything in another async function to get what you expect.

async function main() {
  let quotes;

  const getQuotes = async () => {
    // ...

  await getQuotes(); // await here
  console.log(quotes); // should be set


OK. I thought when i used await the code would hang there until that completed. Would it work if I used

await getQuotes();

I found freecodecamp because of a random interest in Python. I plowed through that and thought the front end stuff would be a breeze. I was wrong! Better not quit my day job :grinning:

You can only use await inside of an async function, so no, you can’t do that (check my second reply).

Thanks Colin, back to the drawing board.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.