Home > Mobile >  async function returns Promise { <pending> }
async function returns Promise { <pending> }

Time:11-08

I am trying to write a function that takes a URL and returns the engagement stats from Facebook Graph API against that URL. But I am facing trouble as my function is just giving me Promise { <pending> } as output.

Here is my code:

const getFacebookStats = async (link, ACCESS_TOKEN) => {
  try {
    const resp = await axios.get(
      `https://graph.facebook.com/v12.0/?id=${link}&fields=engagement&access_token=${ACCESS_TOKEN}`
    );
    return resp;
  } catch (err) {
    // Handle Error Here
    console.error(err);
  }
};

Any help would be much appreciated.

CodePudding user response:

Call your function like this:

getFacebookStats(link, ACCESS_TOKEN).then(response => {
    console.log(response);
}).catch(error => {
    console.error(error);
});

Or you could call it using await inside an async function.

Check this answer for a more detailed explaination of Promises in javascript.

  • Related