Javascript 如何定期检查状态并与请求同步?

Javascript 如何定期检查状态并与请求同步?,javascript,node.js,setinterval,Javascript,Node.js,Setinterval,问题的背景如下: 我在后面写了一份请愿书,这是从一个缓慢的过程开始的,你可以通过一个休息请求来检查 当这个请求表明进程已经完成时,我需要继续使用主线程 所以我做了一个启动进程的请求,在那之后,我想每20秒发出一个请求,以便知道进程是否完成,最后我想继续主线程 为了解决这个问题,我使用了以下代码: ... const checker = await setInterval(() => { let state = checkState(exportId) if(sta

问题的背景如下:

我在后面写了一份请愿书,这是从一个缓慢的过程开始的,你可以通过一个休息请求来检查

当这个请求表明进程已经完成时,我需要继续使用主线程

所以我做了一个启动进程的请求,在那之后,我想每20秒发出一个请求,以便知道进程是否完成,最后我想继续主线程

为了解决这个问题,我使用了以下代码:

 ...
 const checker = await setInterval(() => {
     let state = checkState(exportId)
      if(state !== '100%'){
        state = checkState(exportId)
      }else{
        clearInterval(checker)
        console.log('Clear interval')
      }
 }, 20000);
 ...
检查状态功能如下所示:

checkState = async (exportId: string): Promise<IStatus> => {
    const headers = {...}
    const query = JSON.stringify({...});
    const { _, data } = await axios.post(...);
    return data
  };
checkState=async(exportId:string):Promise=>{
常量头={…}
const query=JSON.stringify({…});
const{,data}=wait axios.post(…);
返回数据
};
这段代码的问题是,当我执行它时,setInterval内的代码永远不会执行

那么,我如何解决这个问题,并等待
setInterval
直到状态返回
100%

谢谢

setInterval
不返回承诺,它返回一个标识符,以便您以后可以取消间隔。等待此值不起任何作用

let state = checkState(exportId)
      if(state !== '100%'){

checkState
是一个异步函数,这意味着它将始终返回一个承诺。它永远不会返回字符串<代码>等待承诺,直到它解析为字符串:

 const checker = setInterval(async () => {
     let state = await checkState(exportId);
      if(state !== '100%'){
        // no need to call checkState because you're using setInterval.
        // state = checkState(exportId)
        console.log('State currently is', state);
      }else{
        clearInterval(checker);
        console.log('Clear interval');
      }
 }, 20000);
或相当于:

 const checker = setInterval(() => {
     checkState(exportId).then((state) => {
      if(state !== '100%'){
        // no need to call checkState because you're using setInterval.
        // state = checkState(exportId)
        console.log('State currently is', state);
      }else{
        clearInterval(checker);
        console.log('Clear interval');
      }
     });
 }, 20000);
您可以将整个过程总结为一个承诺,即在主流程完成后继续:

const finished = new Promise((resolve, reject) => {
 const checker = setInterval(async () => {
     let state = await checkState(exportId);
      if(state !== '100%'){
        // no need to call checkState because you're using setInterval.
        // state = checkState(exportId)
        console.log('State currently is', state);
      }else{
        clearInterval(checker);
        resolve();
        console.log('Clear interval');
      }
 }, 20000);
});

await finished;

console.log("Done");
setInterval
不返回承诺,它返回一个标识符,以便您以后可以取消间隔。等待此值不起任何作用

let state = checkState(exportId)
      if(state !== '100%'){

checkState
是一个异步函数,这意味着它将始终返回一个承诺。它永远不会返回字符串<代码>等待承诺,直到它解析为字符串:

 const checker = setInterval(async () => {
     let state = await checkState(exportId);
      if(state !== '100%'){
        // no need to call checkState because you're using setInterval.
        // state = checkState(exportId)
        console.log('State currently is', state);
      }else{
        clearInterval(checker);
        console.log('Clear interval');
      }
 }, 20000);
或相当于:

 const checker = setInterval(() => {
     checkState(exportId).then((state) => {
      if(state !== '100%'){
        // no need to call checkState because you're using setInterval.
        // state = checkState(exportId)
        console.log('State currently is', state);
      }else{
        clearInterval(checker);
        console.log('Clear interval');
      }
     });
 }, 20000);
您可以将整个过程总结为一个承诺,即在主流程完成后继续:

const finished = new Promise((resolve, reject) => {
 const checker = setInterval(async () => {
     let state = await checkState(exportId);
      if(state !== '100%'){
        // no need to call checkState because you're using setInterval.
        // state = checkState(exportId)
        console.log('State currently is', state);
      }else{
        clearInterval(checker);
        resolve();
        console.log('Clear interval');
      }
 }, 20000);
});

await finished;

console.log("Done");

checkState
是一个异步函数。您必须在
setInterval
的回调函数中等待它。setInterval不知道承诺。实现取决于您没有想到的细节。如果checkState花费的时间超过20秒,该怎么办?是否应该同时有请求?它应该先等待现有的一个,然后再生成另一个吗?是否应取消上一个?
checkState
是一个异步函数。您必须在
setInterval
的回调函数中等待它。setInterval不知道承诺。实现取决于您没有想到的细节。如果checkState花费的时间超过20秒,该怎么办?是否应该同时有请求?它应该先等待现有的一个,然后再生成另一个吗?上一个应该取消吗?