2 回答

TA贡献1847条经验 获得超7个赞
如果您希望能够使用从该类获取的其他类中的数据,Fetch而不是因为该fetchData调用是异步的,则必须在异步调用以异步方式完成后调用所有将使用该数据的代码。
class Fetch {
async fetchData(path) {
const res = await fetch(path)
const {
status
} = res;
if (status < 200 || status >= 300) {
return console.log("Oh-Oh! Seite konnte nicht gefunden werden: " + status)
}
this.data = await res.text();
}
}
class Day08 extends Fetch {
constructor(path) {
super()
this.path = path;
}
async init() {
await this.fetchData(this.path);
}
doSomething() {
console.log('Something', this.data)
}
doSomethingElse() {
console.log('Else ', this.data)
}
}
const day = new Day08('https://jsonplaceholder.typicode.com/todos/1')
day.init().then(() => {
day.doSomething();
day.doSomethingElse();
})

TA贡献1784条经验 获得超9个赞
似乎问题是 Day08 的构造函数必须有数据参数
constructor(data) {
super(data);
this.doSomething();
}
添加回答
举报