如何修复代码块阻塞执行

问题描述 投票:0回答:1
async function getMatchesByFixtureId(id){

  let count = 0;
  let request = true;
  let response;

  const options = {
    method: 'GET',
    url: 'https://api-football-v1.p.rapidapi.com/v3/fixtures',
    params: {id: id},
    headers: {
      'x-rapidapi-key': API_KEY,
      'x-rapidapi-host': 'api-football-v1.p.rapidapi.com'
    }
  };

  async function innerFunction(){
    
    
      if(request === true){

        response = await axios.request(options);
        const match = response.data.response;
        console.log(match);
        let status;
        console.log(1);


              if(["CANC", "FT", "AET", "PEN", "PST", "AWD", "WO"].includes(match.fixture.status.short)){
                status = "FT";
                if(count === 4){
                  request = false;
                }
                count++;
              }
              
              else if(["TBD", "NS"].includes(match.fixture.status.short)){
                status = "SC";
                count = 0;
              }
              else if(["HT", "LIVE", "1H", "2H", "ET", "BT", "P"].includes(match.fixture.status.short)){
                status="LIVE";
                count = 0;
              }
  
          console.log(2);
          
  
        const fixture = {
           Bunch of data
        }
  
        console.log(3);
  
        
        await client.db("db").collection("matches").replaceOne(fixture);

      }

      else{
        return;
      }

  }

  const task = cron.schedule("* * * * *", async() => {
    
    await innerFunction();

  }, {timezone: "UTC"});
  task.start();

  setTimeout(task.stop, 8.1e+6)

}

所以基本上上面的

getMatchesByFixtureId()
是由另一个函数调用的,该函数安排每分钟执行来自
getMatchesByFixtureId()
一场具有匹配赛程 ID 的直播足球比赛的
getMatchesByFixtureId()
api-football
请求。

问题是

console.log(1)
console.log(2)
之间的代码会阻止下面代码的执行,当我将其注释掉时,
console.log(2)
console.log(3)
之间的代码会阻止下面的代码(如果有人想知道是 api 请求的话)工作正常,我收到了正确的数据

javascript node.js mongodb cron node-cron
1个回答
0
投票

问题是我收到的数据在一个数组中,所以我必须选择数组的位置 0。

const match = response.data.response;
->
const match = response.data.response[0];

© www.soinside.com 2019 - 2024. All rights reserved.