简体   繁体   English

第一行完成后如何开始下一行?

[英]How can I start the next row after the first one is completed?

I want to send and receive some information from the CS:GO GC, with my current script I send all request at one time, but I want some delay inbetween these requests (1000 - 2000ms maybe) so that every request is going through.我想从 CS:GO GC 发送和接收一些信息,使用我当前的脚本一次发送所有请求,但我希望在这些请求之间有一些延迟(可能是 1000 - 2000 毫秒),以便每个请求都通过。

The best way for this could be like that the row is starting and when it got the data it requested, then the second row is starting... and so on.最好的方法可能是该行开始,当它获得它请求的数据时,第二行开始......等等。

My current code looks like this (simplified it a bit):我当前的代码看起来像这样(稍微简化了一点):

    pool.getConnection(function(err, connection) {
      if (err) throw err;
      connection.query("SELECT * FROM Users WHERE SteamID64 IS NOT NULL", function (err, rows, fields) {
        connection.release();
        if (err) throw err;
        rows.forEach( (row) => {

      //Request Data from CS:GO
      csgo.requestPlayersProfile(SteamID64, function(ranking) {
          var rankid = ranking.ranking.rank;
          var wins = ranking.ranking.wins;
          var private = ranking.player_level;

          console.log("=================================");
          console.log("SteamID: " + `${row.SteamID64}`);
          console.log("Rank: " + rank);
          console.log("Wins: " + wins);
          console.log("Private Rank: " + private);
          console.log("=================================");
      });
    });
  });
});

I would convert the callback style function to promise style and use async/await .我会将回调样式函数转换为 promise 样式并使用async/await Similarly wrap setTimeout in promise and use that to add wait in between类似地将setTimeout包装在 promise 中,并使用它在两者之间添加等待

async function requestPlayersProfile(SteamID64) {
  return new Promise((resolve, reject) => {
    csgo.requestPlayersProfile(SteamID64, function(ranking) {
      resolve(ranking);
    });
  });
}

async function wait(ms) {
  return new Promise(resolve => {
    setTimeout(resolve, ms);
  });
}

pool.getConnection(function(err, connection) {
  if (err) throw err;
  connection.query("SELECT * FROM Users WHERE SteamID64 IS NOT NULL", async function(
    err,
    rows,
    fields
  ) {
    connection.release();
    if (err) throw err;
    for(const row of rows) {
      const ranking = await requestPlayersProfile(ow.SteamID64);
      var rankid = ranking.ranking.rank;
      var wins = ranking.ranking.wins;
      var private = ranking.player_level;

      console.log("=================================");
      console.log("SteamID: " + `${row.SteamID64}`);
      console.log("Rank: " + rank);
      console.log("Wins: " + wins);
      console.log("Private Rank: " + private);
      console.log("=================================");

      await wait(1000);
    }
  });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果一秒钟内未完成getJSON请求,然后转到下一个,该如何禁用呢? - How can I disable a getJSON request if it hasn't completed in one second and move onto the next one? 我如何确定它首先从“Startpoint” function 调用开始,然后等到该调用解决后再开始下一个 function 调用? - How can I be sure that it first starts with the “Startpoint” function call and waits until this one is resolved to start the next function call? 异步功能完成后,如何开始执行功能? 在这种情况下,诺言不起作用 - How can I start performing function after async function is completed? A promise is not working in this case 可播放:行完成后跳转到下一行的开始 - Handsontable: Jumping to the start of the next row when a row is completed 一旦第一个被清除,我如何开始一个 setInterval ? - How can i start a setInterval once the first one is cleared? 另一个函数完成后,如何执行javascript函数? - How can you execute a javascript function after another one is completed? 如何在调用下一个请求之前完成第一个 Ajax Get 请求? - How can I make first Ajax Get request be finish before I call next one? 页面加载完成后如何执行JavaScript函数? - How can I execute a JavaScript function after Page Load is completed? 仅在递归完成后,如何才能做出Promise解决方案? - How can I make a Promise resolve only after recursion is completed? Facebook.streamPublish()完成后如何重定向? - How can I redirect after Facebook.streamPublish() has completed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM