等待 HTTP 请求中回调函数完成后再继续 for 循环

Wait for completion of callback function in HTTP request before continuing with for loop

我正在尝试使用内置的 HTTPS 库在 node.js 中 运行 3 次 HTTP 请求,每个 运行 都有一组不同的选项。为了在一次执行完成后继续下一组选项,我使用了一个 for 循环,每个 运行 递增一次,总共 3 运行s。但是,相反在循环中继续之前等待每个 运行 完成,它完全跳过前两个调用并 运行 使用第三组选项进行 3 次。我已经寻找了答案,有人有ideas/solutions吗?

    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);
    });
}

但更简洁的方法是使用基于承诺的请求库,例如 fetchaxios