亲爱的人比我聪明(每个人)。我尝试使用NodeJS每20秒拉一次AWS SQS(长时间拉动)。一旦它确实提取了消息,该功能将转换视频,该视频最多可能需要5分钟才能完成。
问题:如果我还在处理视频,我不想拉AWS SQS。所以,如果我的承诺没有解决,我需要我的setInterval函数来跳过aws.SQS.recieveMessage函数。有人知道这样做的好方法吗?
这是我可怕的测试,看看我是否可以理解这些概念(不起作用):
// Quick Testbed
let asyncthingy = asyncCall();
const testTimer = () => {
if (!asyncthingy.promise){
asyncthingy = asyncCall();
}
console.log("I executed the specified function");
}
function resolveAfter4Seconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
},4000);
});
}
async function asyncCall() {
console.log("I'm the async call");
let result = await resolveAfter4Seconds();
}
setInterval(testTimer, 1500);

答案 0 :(得分:0)
经过一些游戏并放弃了以原生方式检查承诺状态的想法,我最终手动设置了状态。如果有人能找到更好的方法,请分享。下面是清理过的测试代码,也应该更加清晰。
// Quick Testbed
var transcoderstatus = 0;
async function startSQSListener() {
if (!transcoderstatus){
transcoderstatus = 1;
let result = await checkSQSQueue();
transcodeVideo();
}
//console.log("Transcoder Status: ",transcoderstatus);
}
function checkSQSQueue() {
console.log("Checking SQS Queue");
return new Promise(resolve => {
setTimeout(() => {
console.log("Pulled new message");
resolve('resolved');
},2000);
});
}
function transcodeVideo() {
transcoderstatus = 1;
console.log("Transcoding Video");
return new Promise(resolve => {
setTimeout(() => {
transcoderstatus = 0;
console.log("Transcoding completed");
console.log("---------------");
resolve('resolved');
},10000);
});
}
setInterval(startSQSListener, 1500);