在失败之前轮询结果 n 次(尝试之间有延迟)
Poll for a result n times (with delays between attempts) before failing
我们需要编写一个 Node.js 函数来轮询某个 API 端点以获取先前请求的计算结果。结果需要随机时间生成,可能根本不是我生成的。我们希望尽快得到它,但我也不想等待太久,这意味着在一定数量的 API 调用后我们希望函数失败(拒绝一个承诺)。
我们的代码和 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 的 async/await 版本!它甚至不会在您调用 resolve
时停止循环,或者在抛出异常时拒绝(例如,当 res
为 null
时)。
你应该使用
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 承诺,而不是本地承诺,请将其包装在 Bluebird.method
中或告诉您的转译器使用 Bluebird。
我们需要编写一个 Node.js 函数来轮询某个 API 端点以获取先前请求的计算结果。结果需要随机时间生成,可能根本不是我生成的。我们希望尽快得到它,但我也不想等待太久,这意味着在一定数量的 API 调用后我们希望函数失败(拒绝一个承诺)。
我们的代码和 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 的 async/await 版本!它甚至不会在您调用 resolve
时停止循环,或者在抛出异常时拒绝(例如,当 res
为 null
时)。
你应该使用
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 承诺,而不是本地承诺,请将其包装在 Bluebird.method
中或告诉您的转译器使用 Bluebird。