2

I want to call an async function making an API call inside another function.

Specifically in the code below I am defining an asynchronous function verifyEmail making an API call to a verification service for mail adresses. The function is working when I test it with a random email.

I would like to be able to call this function inside the getProfileInfos function that parses a json file and return an array of objects. The verifyEmail(result.dropcontact.email)is wrong since it will always return the non-resolved Promise. I did some research but do not find the right syntax to call the async function and wait for the result to set the email in my object.

Any help is appreciated!

const verifyEmail = async (email) => {
  const response = await fetch(`https://apilayer.net/api/check?access_key=******&email=${email}`);
  const data = await response.json();
  if (data.format_valid && data.score > 0.5) {
    return data.email;
  };
}

const getProfileInfos = (data) => {
  const jsonData = JSON.parse(data);
  const results = jsonData.map((result) => {
    if (result.general) {
      const firstName = result.general.firstName;
      const lastName = result.general.lastName;
      const company = result.jobs[0].companyName;
      const jobTitle = result.jobs[0].jobTitle;
      const email = result.dropcontact ? verifyEmail(result.dropcontact.email) : undefined;
      return {
        firstName,
        lastName,
        company,
        jobTitle,
        email
      }
    }
  });
  return results;
}
3
  • you will also have to await the verifyEmail function, which also makes your getProfileInfos function an async function Commented Jul 24, 2019 at 21:05
  • Waiting for the result of a promise is done with the await keyword or .then() method. There is no wait to wait synchronously (blocking). Commented Jul 24, 2019 at 21:22
  • Thank you for the answer, I've tried to write it different (comments on next answer) but still not able to get the result of the promise inside the function. Commented Jul 24, 2019 at 21:32

2 Answers 2

1

Add async keyword to getProfileInfos and change line const email = ... to

const email = result.dropcontact ? (await verifyEmail(result.dropcontact.email)) : undefined;
Sign up to request clarification or add additional context in comments.

Comments

1

Try to define async inside the getProfileInfos props and invoke await before verifyEmail

e.g.

const getProfileInfos = async (data) => {
  const jsonData = JSON.parse(data);
  const results = jsonData.map((result) => {
    if (result.general) {
      const firstName = result.general.firstName;
      const lastName = result.general.lastName;
      const company = result.jobs[0].companyName;
      const jobTitle = result.jobs[0].jobTitle;
      const email = result.dropcontact ? await verifyEmail(result.dropcontact.email) : undefined;
      return {
        firstName,
        lastName,
        company,
        jobTitle,
        email
      }
    }
  });
  return results;
}

5 Comments

Thank you for the quick answer. I'm getting a syntax error, I guess it is because in this case const results = jsonData.map((result)... should be the async function. But when I try jsonData.map(async (result)... I just get an array of Promise { <pending> }
try to console.log the jsonData and check if the array is fetching.
Probably you need to use .then() to get these data, if the Promise is pending something javascript is waiting for then().
Yes it is, the code works as expected when I don't use the verifyEmail function. const email = result.dropcontact ? result.dropcontact.email : undefined; allows me to get the object I want.
const email = result.dropcontact ? await verifyEmail(result.dropcontact.email).then(email => email) : undefined; I tried this as well but still not returning the verified email

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.