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

158
Views
Await fetch not working inside class constructor

This code is working fine except it is not waiting for the fetch to be finished causing undefined references to rendimiento.MesHora after Rendimiento object creation using new instruction.References to other rendimiento properties works fine and

export default class Rendimiento {

constructor(lon, lat, inclinacion, azimuth, perdidas) {

    let fromPVGIS = this.loadRendimiento(lon, lat, inclinacion, azimuth, perdidas);
    this.MesHora = fromPVGIS.rendimientoMesHora;
    this.lon = lon;
    this.lat = lat;
    this.inclinacion = fromPVGIS.inclinacion;
    this.azimuth = fromPVGIS.azimuth;
    this.perdidas = fromPVGIS.perdidas;
};

async loadRendimiento(lon, lat, inclinacion, azimuth, perdidas) {
    const nombreMes = ["Ene","Feb","Mar","Abr","May","Jun","Jul","Ago","Sep","Oct","Nov","Dic"];

    let _PVGIS = {inclinacion: 37., azimuth: -3., perdidas: 0.14, rendimientoMesHora: Array.from(Array(12), () => new Array(24).fill(0))}; 

    var url = './Datos/PVGIS Original.json';

    var _rendimientoMesHora = Array.from(Array(12), () => new Array(24).fill(0));
    var jsonData;
    var hora;
    var mes;

    try {
        const response = await fetch(url);
        if (response.status === 200) {
            var data = await response.text();
            jsonData = JSON.parse(data);
            for ( hora = 0; hora < 24; hora++) {
                for ( mes=0; mes<12; mes++) {
                    _rendimientoMesHora[mes][hora] = jsonData[hora][nombreMes[mes]] / 1000.;
                }
            }
            _PVGIS.rendimientoMesHora = _rendimientoMesHora;
            return (_PVGIS);
        } else {
            console.log(response);
        }
    } catch (err) {
            console.log(err);
            throw err; 
    };
};

};

where is the mistake?

about 4 years ago · Juan Pablo Isaza
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!