这是我的提取函数:
getAllCountries = async () => {
try {
const response = await fetch("https://restcountries.eu/rest/v2/all");
const result = response.json();
console.log(result);
this.countriesList = result;
} catch (error) {
console.log(error);
}
};
为什么在那里登录了两个Promise,以及如何访问PromiseResult。
我尝试了console.log(result[0])
,但没有成功
答案 0 :(得分:6)
json()
还返回一个承诺,所以:
public static bool IsIEnumerableOfT(this Type type)
{
return type.GetInterfaces().Any(x => x.IsGenericType
&& x.GetGenericTypeDefinition() == typeof(IEnumerable<>));
}
侧面说明:该代码已沦为 getAllCountries = async () => {
try {
const response = await fetch("https://restcountries.eu/rest/v2/all");
const result = await response.json();
// ^^^^^−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− add
console.log(result);
this.countriesList = result;
} catch (error) {
console.log(error);
}
};
API步枪的猎物:fetch
仅拒绝针对 network 错误而不是HTTP错误的承诺。您必须自己检查这些内容,例如:
fetch