Skip to content

I keep getting undefined after async call in another function

I Have 2 functions one which uses async and await to grab data and place it into an array. The second is acts like a checker to see if the user inputs a similar value as seen on the database

function repeatsChecker() {
  let api_data_values = []

  fetchData().then(data => {
    for (let i = 0; i < data.length; i++) {

  return api_data_values


// testing for similarities

async function test() {
  let name = "Joe"
  let test = await repeatsChecker();


When I compile a simple if statement everything returns true and when I do console.log(test[0]) it returns undefined?


repeatChecker isn’t returning a promise, so the fact that you’re awaiting doesn’t have any meaningful effect. console.log(test[0]) executes before api_data_values.push(data[i].name).

Try this:

function repeatsChecker() {
  return fetchData().then(data => {
    return =>;

Or with async/await:

async function repeatsChecker() {
  const data = await fetchData();
  return =>;