Please be informed that you are just spamming around.
And your solution is not good at all. You cannot super() in the constructor and you cannot extends the class. And your constructor is not a constructor, which is harmful.
Developer advocate, full-stack engineer, startup co-founder & CTO, bringing 15 years of experience in Silicon Valley, including at Google and Yahoo!. Public speaker.
Location
🌐
Education
UC Santa Cruz Extension
Work
Developer Advocate at Weaviate, the open-source semantic search engine
Did you mean that more precisely, you can call super in the constructor; you can't await super() in derived class constructors, but you can extend the class if you don't need to overload the constructor? I've documented this downside in the SO answer.
Can you expand on how the constructor is not a constructor?
Pedantically, I am on the camp that constructors should never return anything at all. That's just an unexpected quirk of JavaScript that even tsserver trips up on.
Semantically, constructors initialize fields, not return values. Thus, returning a Promise from a constructor is not only awkward, but also unexpected because it returns something aside from the intended class instance (i.e., Promise<Animal> vs. Animal itself).
A static async factory method makes this distinction well: do the async work ahead of time and then invoke the constructor with the now-resolved field initializers.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Please be informed that you are just spamming around.
And your solution is not good at all. You cannot
super()
in the constructor and you cannotextends
the class. And your constructor is not a constructor, which is harmful.Did you mean that more precisely, you can call
super
in the constructor; you can'tawait super()
in derived class constructors, but you can extend the class if you don't need to overload the constructor? I've documented this downside in the SO answer.Can you expand on how the constructor is not a constructor?
A constructor should anyway return the constructed object itself, instead of a promise resolving to the said object, unless the object is the promise.
we are discussing creating objects asyncronously, in that case the constructor HAS to return a promise.
Pedantically, I am on the camp that constructors should never return anything at all. That's just an unexpected quirk of JavaScript that even
tsserver
trips up on.Semantically, constructors initialize fields, not return values. Thus, returning a
Promise
from a constructor is not only awkward, but also unexpected because it returns something aside from the intendedclass
instance (i.e.,Promise<Animal>
vs.Animal
itself).A static async factory method makes this distinction well: do the
async
work ahead of time and then invoke theconstructor
with the now-resolved field initializers.