【发布时间】:2020-08-22 17:24:12
【问题描述】:
我有一个父类 Weather,它显示了 4 天的天气。我有一个子类,其中包含一系列城市。他应该在这个数组中显示一个随机城市的 4 天的天气。但我有一个错误 - this.makeRandom(...).then 不是函数。怎么了?
class GetRandomCity extends Weather {
constructor(city) {
super();
this.city = city;
}
makeRandom() {
//code
};
init() {
this.makeRandom().then(city => {
this.getCoordinates(city)
}).then(coords => {
return this.getWeatherForecast(coords);
}).then((forecast) => {
const { currently, daily } = forecast;
this.renderForecastInfo(currently, daily);
});
}
}
const w1 = new GetRandomCity(['Paris', 'Minsk', 'Madrid', 'Chikago']);
【问题讨论】:
-
makeRandom 不是异步函数,then 不需要,then 用于promise。
-
@BerkKurkcuoglu 如何将城市值传递给函数 getCoordinates?
-
this.getCoordinates(this.makeRandom())
标签: javascript arrays class asynchronous promise