我试图使用内置的HTTPS库在node.js中运行三次HTTP请求,每次运行都有一组不同的选项。为了在完成一次执行之后继续到下一组选项;m使用for循环,每次运行递增一次,总共运行3次。然而,它没有等待每次运行完成后再继续循环,而是完全跳过前两个调用,并使用第三组选项运行3次。我到处寻找答案,有人有什么想法/解决方案吗?
for(let i = 0; i < 3; i++) {
option = i;
console.log("Calling with option " + i)
await http.request(options[i], res => {
let resp = '';
res.on('data', function(chunk) {
resp += chunk;
});
res.on('end', async function () {
let data = await JSON.parse(resp);
if(option === 0) { //Bloxlink handler
console.log("Case 1: Bloxlink")
if(data.status === "error") {
returndata += "Bloxlink returned no users"
} else {
returndata += "nBloxlink returned user with ID " + data.primaryAccount + " and username " + await noblox.getUsernameFromId(parseInt(data.primaryAccount)) + "."
}
} else if(option === 1) { //RoWifi handler
console.log("Case 2: RoWifi")
if(data.success === false) {
returndata += "RoWifi returned no users"
} else {
returndata += "nRoWifi returned user with ID " + data.roblox_id + " and username " + await noblox.getUsernameFromId(data.roblox_id) + "."
}
} else if(option === 2) { //Eryn handler
console.log("Case 3: Eryn")
if(data.status === "error") {
returndata += "Eryn returned no users"
} else {
returndata += "nEryn returned user with ID " + data.robloxId + " and username " + data.robloxUsername + "."
}
}
console.log(returndata);
});
res.on('error', function () {
channel.send('One or more APIs returned an error. The operation has been terminated.')
});
}).end();
channel.send("Run " + i + " finished: " + returndata);
console.log(returndata);
}
您可以将其封装在promise中,然后等待它:
for(let i = 0; i < 3; i++) {
option = i;
console.log("Calling with option " + i)
await new Promise((resolve, reject) => {
http.request(options[i], res => {
let resp = '';
res.on('data', function(chunk) {
resp += chunk;
});
res.on('end', async function () {
let data = await JSON.parse(resp);
if(option === 0) { //Bloxlink handler
console.log("Case 1: Bloxlink")
if(data.status === "error") {
returndata += "Bloxlink returned no users"
} else {
returndata += "nBloxlink returned user with ID " + data.primaryAccount + " and username " + await noblox.getUsernameFromId(parseInt(data.primaryAccount)) + "."
}
} else if(option === 1) { //RoWifi handler
console.log("Case 2: RoWifi")
if(data.success === false) {
returndata += "RoWifi returned no users"
} else {
returndata += "nRoWifi returned user with ID " + data.roblox_id + " and username " + await noblox.getUsernameFromId(data.roblox_id) + "."
}
} else if(option === 2) { //Eryn handler
console.log("Case 3: Eryn")
if(data.status === "error") {
returndata += "Eryn returned no users"
} else {
returndata += "nEryn returned user with ID " + data.robloxId + " and username " + data.robloxUsername + "."
}
}
console.log(returndata);
resolve();
});
res.on('error', function () {
channel.send('One or more APIs returned an error. The operation has been terminated.')
reject();
});
}).end();
}).then(() => {
channel.send("Run " + i + " finished: " + returndata);
console.log(returndata);
});
}
但是,更干净的方法是使用基于承诺的请求库,如fetch
或axios
。