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();
}
添加回答
舉報