0

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 ...
J.Todd
  • 707
  • 1
  • 12
  • 34
  • 1
    Why not first read the file and then pass the data to the constructor? Or you can create a static method that reads the file, creates a instance and returns it. – Yousaf Feb 02 '22 at 05:15
  • Related: [Async/Await Class Constructor](https://stackoverflow.com/a/43433773) – SuperStormer Feb 02 '22 at 05:17
  • Related: [Asynchronous operations in constructor](https://stackoverflow.com/questions/49905178/asynchronous-operations-in-constructor/49906064#49906064). Basically, you can't make a constructor work properly with asynchronous operations in it because it has to return the instance of the class, not a promise so the caller has no idea when it's actually done. A factory function that returns a promise whose resolved value is the object instance is a better suited design pattern and then you can use as many asynchronous operations as you want in the factory function. So, change your design. – jfriend00 Feb 02 '22 at 06:11

0 Answers0