Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

273
Visualizações
What's the proper way to extend a class in JavaScript if super() needs to wait for a promise?

I wrote an unusual class where I imagined I would be able to load a JSON file to fulfill many of the class's settings and initiate an object accordingly. Except, what happens is the function returns asynchronously while we wait for the filesystem promise (and that's a no-no when extending a class, since super() must be called). Sure, I could go synchronous with the fs call but that seems like a bandaid. What's the right way to go about this?

module.exports = class EntityLoader extends Entity {
    constructor(args, jsonPath="./web/classes/objects/box-pile.json"){
        fs.promises.readFile(jsonPath, 'utf8').then(data => {
            const settings = JSON.parse(data)
            super({
                ...settings,
                ...args
            })
            this.ready.then(() => {
                if (settings.children) settings.children.forEach(childSettings => {
                    this.addChildSprite({...childSettings}).position.set(...childSettings.position[0])
                })
            })
        });
    }
}

Error:

... snip ...\classes\objects\box-pile.js:31 Uncaught ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor
    at new EntityLoader ... snip ...
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda