Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
Async Function that returns Array, when accessed, returns a non-iterable Object

Quokka Output of my code

I have created a script meant to read data from a google sheet, the function getData() returns a promise of an Array. However, when I access the data afterwards, the value of the data is a promise of a function. Why is this the case?


import dotenv from "dotenv";
import { google } from "googleapis";
import { mainModule } from "process";

dotenv.config();

const spreadsheetID = process.env.spreadsheetID;

if (process.env.NODE_ENV == "production") {
  var fs = require("fs");

  fs.writeFile("./secrets.json", process.env.SECRET, (err: any) => {
    err;
  });
}

async function getData() {
  const auth = new google.auth.GoogleAuth({
    keyFile: "secrets.json",
    scopes: "https://www.googleapis.com/auth/spreadsheets",
  });

  const client = await auth.getClient();

  const googleSheets = google.sheets({ version: "v4", auth: client });

  var getRows = await googleSheets.spreadsheets.values.get({
    auth: auth,
    spreadsheetId: spreadsheetID,
    range: "Sheet1",
  });
  console.log(getRows.data.values);
  console.log(Array.isArray(getRows.data.values))
  return getRows.data.values;
}

const data = getData();

data.then(() => {
  console.log(Array.isArray(data);
    console.log(data);
    
  var present_list = (data as any).forEach((user: any) => {
    if (user[4] == "P") {
      present_list.append(user);
    }
  });
  console.log(present_list);
});

console.log("asdasd");

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If we want to access the data from a promise, we would want to do something like this:

//...
const data = getData();

data.then((result) => {
  const present_list = result.forEach((user: any) => {
    ...
  });
});

We don't want to use the data variable, because obviously that would a promise, even inside the callback function passed to then. We would want to use the parameter of the callback, which will be have the data extracted from the promise.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda