Issue
I have some setup I want during a constructor, but it seems that is not allowed
Which means I can't use:
How else should I do this?
Currently I have something outside like this, but this is not guaranteed to run in the order I want?
async function run() {
let topic;
debug("new TopicsModel");
try {
topic = new TopicsModel();
} catch (err) {
debug("err", err);
}
await topic.setup();
Solution
A constructor must return an instance of the class it 'constructs'. Therefore, it's not possible to return Promise<...>
and await for it.
You can:
Make your public setup
async
.Do not call it from the constructor.
Call it whenever you want to 'finalize' object construction.
async function run() { let topic; debug("new TopicsModel"); try { topic = new TopicsModel(); await topic.setup(); } catch (err) { debug("err", err); } }
Answered By - Amid
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.