Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!