0

I need some help, I went to almost all questions here and in MDN and I couldn't find a solution for my problem. For sure it's here somewhere but I couldn't see it.

const fetch = require("node-fetch");

const url = 'https://myapi.com'

const getData = async () => {
  const data = await fetch(url);
  const dataStage1 = await data.json();
  let finalData = dataStage1.data[0];
  return finalData;
};


const data = getData();

console.log(data)

output: Promise { <pending> }

I can't just const data = getData(); because I get SyntaxError: await is only valid in async function

I really don't understand what I'm doing wrong, in getData() function I'm waiting for the results and returning the object I need.

I need to have this object in a variable because I need to manipulate it.

Thank you o much for any help explanation, I'm having a really hard time to understand why this is happening

  • 2
    Async functions always return a promise. That's how they work. You need to await that promise or use the promise API: `getData().then(data => console.log(data))`. – VLAZ May 12 '21 at 07:21
  • Have you tried `const data = await getData();` – Molda May 12 '21 at 07:24
  • @VLAZ I need to have the return object in a variable, no just console.log it. `getData()` is returning the object I need, and I would like to assign this object to a `const` to manipulate it. How I can do that? Thank you for your comment and time – Caio César P. Ricciuti May 12 '21 at 07:30
  • @Molda `const data = await getData();` == `^^^^^SyntaxError: await is only valid in async function` – Caio César P. Ricciuti May 12 '21 at 07:30
  • @CaioCésarP.Ricciuti your two choices are: use `await` or use the Promise API. So, either call `getData()` in an async function -> `await` it -> work with the result that way. Or use `.then(data => { /* put your code here */})`. You can also see [How can I use async/await at the top level?](https://stackoverflow.com/q/46515764) but it seems your environment doesn't support top-level awaits. Otherwise, it's basically the same as the first approach with the async function. – VLAZ May 12 '21 at 07:48

1 Answers1

1

At this step, you are logging an unresolved async function. That's why you get Promise { <pending> }.

const data = getData();

console.log(data)

If you want to get a result to log you have to use await before getData().

const myFunction = async () {
   const data = await getData();
   console.log(data);
}

myFunction();

I can give you an easy async/await example:

const promisifiedFunc = () => {
    return new Promise((resolve, reject) => {

        // This function will be executed after 1.5 seconds
        setTimeout(() => {
            resolve("Success!");
        }, 1500);

    });
}

const myFunc = async () => {
    console.log(`Without await: ` + promisifiedFunc());
    console.log(`With await: ` + await promisifiedFunc());
};

myFunc();

If we use a function with promise structure without await, as in the example, we are trying to get an output a function that will be executed after 1.5 seconds before it finishes. That's why we see Promise { <pending> }

Murat Colyaran
  • 2,075
  • 2
  • 8
  • 27