Javascript 获取 return true 并调用非 returning 结果
Javascript fetch return true and call not returning result
我正在使用 fetch 库,我正在尝试这样做:
fetchtest() {
fetch('someurl...').then((data) => {
if (data.status === 200) {
return true;
}
});
}
然后调用它:
myMethod() {
if (this.fetchtest()) {
console.info('Would return true');
}
}
它没有返回任何东西。
我做错了什么?
发生这种情况是因为 this.fetchtest()
正在返回一个本质上 'truthy' 的 Promise。
它 returns 一个承诺,因此它可能在需要使用时不存在。
我正在使用 fetch 库,我正在尝试这样做:
fetchtest() {
fetch('someurl...').then((data) => {
if (data.status === 200) {
return true;
}
});
}
然后调用它:
myMethod() {
if (this.fetchtest()) {
console.info('Would return true');
}
}
它没有返回任何东西。
我做错了什么?
发生这种情况是因为 this.fetchtest()
正在返回一个本质上 'truthy' 的 Promise。
它 returns 一个承诺,因此它可能在需要使用时不存在。