我们需要编写一个 Node.js 函数来轮询某个 API 端点以获取先前请求的计算结果。结果需要随机时间生成,可能根本不是我生成的。我们希望尽快得到它,但我也不想等待太久,这意味着在一定数量的 API 调用之后我们希望函数失败(拒绝 Promise)。
我们的代码和 API 之间只有一种通信方式。
const Bluebird = require('bluebird');
function getResult() {
return new Bluebird(async function (resolve, reject) {
let counter = 0;
while (counter < 10) {
await Bluebird.delay(1000);
const res = await apiCall();
if (res.data) {
resolve(res.data);
} else {
counter += 1;
}
}
reject('timeout');
});
}
这是正确的方法吗?
最佳答案
没有。这是 Promise
constructor antipattern 的异步/等待版本!它甚至不会在您调用 resolve
时停止循环,或者在抛出异常时拒绝(例如,当 res
为 null
时)。< br/>
你应该使用
async function getResult() {
for (let counter = 0; counter < 10; counter += 1) {
await Bluebird.delay(1000);
const res = await apiCall();
if (res.data) {
return res.data;
}
}
throw new Error('timeout');
}
如果您想确保返回的是 Bluebird promise ,而不是本地 promise ,请将其包装在 Bluebird.method
中或告诉您的转译器使用 Bluebird。
关于javascript - 在失败之前轮询结果 n 次(尝试之间有延迟),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39409655/