[英]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.