async constructor
code:js
class AsyncClass {
constructor() {
console.debug("start constructing");
return new Promise(
(resolve) => setTimeout(() => {
console.debug("constructed!");
resolve(this);
}, 1000)
);
}
}
const cls = await new AsyncClass();
console.debug(cls)
ほんとだ。こんなことできるんだtakker.icon
TypeScript
new()=>Tをnew () => Promise<T>に無理やり型変換するといける ただし、propsのない場合のみ
code:ts
class AsyncClass {
constructor() {
console.debug("start constructing");
return new Promise<this>(
(resolve) => setTimeout(() => {
console.debug("constructed!");
resolve(this);
}, 1000)
);
}
}
const Constructor: new () => Promise<AsyncClass> = AsyncClass as unknown as new () => Promise<AsyncClass>;
const cls = await new Constructor();
console.log(cls)