我正在发出包含大量数据的发布请求,大约需要一分钟才能完成保存。我为此使用的托管服务将在 5 秒后请求超时,因此我需要对其进行设置以定期检查数据保存是否完成以提供最终更新。
我正在使用request-promise
,并且研究了setTimeout
和setInterval
方法。在我最近的尝试(如下)中,我使用了 setTimeout
方法,但我的第二个 then
几乎立即被调用。我希望它在第一个 then
阶段挂起,直到它被检查了很多次(此处为 24 次)或实际完成。
我在这里可能有一个完全错误的方法,但我没有找到我试图引用的例子。任何有关这方面的好例子或我出错的地方的指示将不胜感激。
const request = require('request-promise');
function checkFiles () {
return request({
uri: `${process.env.ROOT_URL}/api/v1/get/file-processing`,
method: 'GET',
json: true
})
.then(res => { return res; })
.catch(err => { return err; });
}
async function init () {
const filesPostOptions = {/* request options */};
await request(filesPostOptions)
.then(async status => { // THEN #1
if (status.status === 201) {
return status;
}
let checks = 0;
const checkIt = function() {
checks++;
checkFiles()
.then(res => {
if (res.status === 201 || checks > 24) {
status = res;
return status;
} else {
setTimeout(checkIt, 5000);
}
})
.catch(err => {
console.error(err);
});
};
checkIt();
})
.then(status => { // THEN #2
if (!status.status) {
throw Error('Post request timed out.');
}
return status;
})
.catch(err => {
err = err.error ? err.error : err;
console.error(err);
});
}
发布响应将传递带有 status
属性(状态代码)和 message
属性的响应。
最佳答案
您需要通过添加Promise
来控制“THEN #”
中的返回:
.then(async status => { // THEN #1
return new Promise((resolve, reject) => { // <---- prevent an immediate return
if (status.status === 201) {
return resolve(status);
}
let checks = 0;
const checkIt = function() {
checks++;
checkFiles()
.then(res => {
if (res.status === 201 || checks > 24) {
status = res;
resolve(status);
} else {
setTimeout(checkIt, 1000);
}
})
.catch(err => reject(err));
};
checkIt();
})
})
关于javascript - 发布请求后,继续检查是否完成 | Node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52300780/