【发布时间】:2018-10-22 01:34:04
【问题描述】:
我有这个函数,如果它失败重试,如果失败 x 次,它应该最终拒绝 Promise。我已经实现了如下:
examplefunction(retries = -1) {
return new Promise(async (resolve, reject) => {
try {
const url = `example.org`;
const json = await this._sendRequest(url);
resolve(json);
} catch (e) {
if( retries > 0 ) {
return this.examplefunction(retries - 1);
}
else {
reject("Unable to communicate with Server. Please try again later!");
}
}
});
}
函数是这样调用的:
backend.examplefunction(3).then(
(json) => {
console.log(json);
},
(reason) => {
console.log.(reason);
}
)
此代码在 React 上下文中执行,因此它也通过 babel transpilitaion 运行。
我的问题是,当它在 x 重试后最终拒绝时,这会导致 Uncaught Promise 错误:
Uncaught (in promise) Unable to communicate with Server. Please try again later!!
谁能向我解释为什么会这样?
【问题讨论】:
-
在抛出异常之前是否记录原因(“错误”)?
-
是的,看我添加的截图
标签: javascript node.js reactjs