Promise.race 触发 "timeout is not defined"
Promise.race fires "timeout is not defined"
这是我的 React 本机应用程序代码,带有登录表单以发送 API 调用。登录按钮的事件处理程序将调度 apiLogin-Action:
try {
await Promise.race([
dispatch(actions.doApiLogin(this.state.username, this.state.password)),
timeout(15000),
]);
} catch (e) {
const message = e.message || e;
if (message !== 'Timed out' && message !== 'Canceled by user') {
alert(message);
console.warn(e);
}
return;
} finally {
this._isMounted && this.setState({isLoading: false});
}
它抛出一个异常"timeout is not defined"。此代码取自 Facebook F8-Application。我无法找到有关 Promise.race-Method 的详细信息,这些信息在互联网上以这种方式处理,但 facebook 运行一个应用程序......
超时有什么问题?
这跟Promise.race
没有关系,可能是你忘了定义函数timeout
,source code
中函数是这样定义的
async function timeout(ms: number): Promise {
return new Promise((resolve, reject) => {
setTimeout(() => reject(new Error('Timed out')), ms);
});
}
如您所见,这是一个简单的承诺,在 ms 毫秒过去后被拒绝,Promise.race
是 resolved/rejected,第一个承诺是 resolved/rejected
这是我的 React 本机应用程序代码,带有登录表单以发送 API 调用。登录按钮的事件处理程序将调度 apiLogin-Action:
try {
await Promise.race([
dispatch(actions.doApiLogin(this.state.username, this.state.password)),
timeout(15000),
]);
} catch (e) {
const message = e.message || e;
if (message !== 'Timed out' && message !== 'Canceled by user') {
alert(message);
console.warn(e);
}
return;
} finally {
this._isMounted && this.setState({isLoading: false});
}
它抛出一个异常"timeout is not defined"。此代码取自 Facebook F8-Application。我无法找到有关 Promise.race-Method 的详细信息,这些信息在互联网上以这种方式处理,但 facebook 运行一个应用程序......
超时有什么问题?
这跟Promise.race
没有关系,可能是你忘了定义函数timeout
,source code
async function timeout(ms: number): Promise {
return new Promise((resolve, reject) => {
setTimeout(() => reject(new Error('Timed out')), ms);
});
}
如您所见,这是一个简单的承诺,在 ms 毫秒过去后被拒绝,Promise.race
是 resolved/rejected,第一个承诺是 resolved/rejected