【发布时间】:2021-06-03 04:36:21
【问题描述】:
我是 Node.js 和 Javascript 的新手,我使用 npm package retry 向服务器发送请求。
const retry = require('retry');
async function HandleReq() {
//Some code
return await SendReqToServer();
}
async function SendReqToServer() {
operation.attempt(async (currentAttempt) =>{
try {
let resp = await axios.post("http://localhost:5000/api/", data, options);
return resp.data;
} catch (e) {
if(operation.retry(e)) {throw e;}
}
});
}
我得到空响应,因为 SendReqToServer 在传递给 operation.attempt 的函数解析承诺之前返回了一个承诺。
如何解决这个问题?
【问题讨论】:
-
SendReqToServer不返回任何内容,这是故意的吗? -
return resp.data将数据返回给.attempt回调。您需要返回operation.attempt(...)以将值返回到SendReqToServer。 -
@evolutionxbox
SendReqToServer返回一个承诺。 -
@ThomasSablik 是的,但在调用
operation.attempt()后它会立即解决 - 实际上不需要等待任何东西 - 因为函数末尾基本上有一个隐含的return undefined;。 -
@ThomasSablik 确实如此。 - OP:
SendReqToServer解析为undefined,我认为这不是故意的
标签: javascript node.js asynchronous async-await callback