post 请求后,继续检查是否完成 | Node.js
After post request, continue checking for completion | Node.js
我正在发出一个 post 请求,其中包含大量数据,大约需要一分钟才能完成保存。我为此使用的托管服务将在 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);
});
}
post 响应将发送一个带有 status
属性(状态代码)和 message
属性 的响应。
您需要通过添加 Promise
:
来控制 "THEN #"
中的 return
.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();
})
})
我正在发出一个 post 请求,其中包含大量数据,大约需要一分钟才能完成保存。我为此使用的托管服务将在 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);
});
}
post 响应将发送一个带有 status
属性(状态代码)和 message
属性 的响应。
您需要通过添加 Promise
:
"THEN #"
中的 return
.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();
})
})