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

299
Views
put an async await on an export

I want to export a variable, but it is being exported empty. I need this to wait for the data to be inserted and then it will be exported, but I'm not able to do it

let eventGuid = 0
let INITIAL_EVENTS = [];
let datas = ""; 

function getDatas(){
  return fetch("http://localhost:3214/events")
    .then((data) => data.json())
    .catch((err) => console.log(err))
}

async function armazenar() {
  datas = await getDatas();
} 


async function api(){
  await armazenar()

  datas.forEach(element => {
    INITIAL_EVENTS.push(element)
  });

}
api()

export default INITIAL_EVENTS;

export function createEventId() {
  return String(eventGuid++)
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

async function api(){
  await armazenar()
  datas.forEach(element => {
    INITIAL_EVENTS.push(element)
  });

}
api()

this function is an async function api(), but has no await api(). Idk if this solves your problem but you should fix it.

about 4 years ago · Juan Pablo Isaza Report

0

First, some explanation about the export statement is necessary. What export is intended for is make code available to other files in the course of the build performed by node. What export won't do is provide values of variables to code in other files. In short, export is not a dynamic feature which operates at runtime.

I would just export an async default function as follows.

let eventGuid = 0

function getDatas(){
  return fetch("http://localhost:3214/events")
    .then((data) => data.json())
    .catch((err) => console.log(err))
}

export default async function () {
  return await getDatas();
} 


export function createEventId() {
  return String(eventGuid++)
}

Now you need to import our default function in some other file and then to execute it. Note that such an async function returns a promise. Your array with initial events is going to be the result returned by the latter promise when it resolves.

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!