如何等待多个请求被解决以在 javascript 中发送响应?

How wait several requests are resolved to send an response in javascript?

我需要使用不同的参数(node.js 和 express.js)对外部 api 执行相同的请求 如果对我的请求的所有回答都是正确的,那么我将发送 angular 客户端代码 200,但是,如果有任何错误,请将错误发送给他。 我尝试了类似的方法但不起作用:

for(var i = 0; i<array.length; i++){
github.repos.createHook({

                    user: user,
                    repo: array[i],
                    name: "web",
                    config: {

                        url: "http://9ec2067d.ngrok.io/api/v1/callback",
                        content_type: "json"
                    },
                    events: ["*"],
                    active:true,

                    headers: {
                        "X-GitHub-OTP": "two-factor-code"
                    }


                }, function(err, res) {
                    if (err) {

                        console.log(err);
                        errGithub = true;
                        response.status(404).json(err);


                    }
                    else {
                        if(i==array.length && !errGithub){


                            response.json({message:"OK"});

                        }


                    }

有什么想法吗? 非常感谢

试试这个:

// Include the async package
// Make sure you add "async" to your package.json
async = require("async");

// 1st para in async.each() is the array of items
async.each(array,
  // 2nd param is the function that each item is passed to
  function(item, callback){
    // Call an asynchronous function,
    github.repos.createHook({

                    user: user,
                    repo: item,
                    name: "web",
                    config: {

                        url: "http://9ec2067d.ngrok.io/api/v1/callback",
                        content_type: "json"
                    },
                    events: ["*"],
                    active:true,

                    headers: {
                        "X-GitHub-OTP": "two-factor-code"
                    }

                }, function(err, res) {
                    if (err) {
                        console.log(err);
                        errGithub = true;
                    }
                    callback(); //required
                });
  },

  // 3rd param is the function to call when everything's done
  function(err){

    if(err){
      console.log('Error:' + err);
    }

    // All tasks are done now
    if(errGithub ===true){
      response.status(404).json(err);
    }else{
      response.json({message:"OK"});
    }
  }
);

您可以使用 Promises,这是一种干净而简单的方法。

首先用 promises 创建一个队列:

   var queue = array.map((item) => { 
      return new Promise((resolve, reject) => {
        github.repos.createHook({ 
            user: user,
            repo: item, 
            name: "web",
            config: {
                url: "http://9ec2067d.ngrok.io/api/v1/callback",
                content_type: "json"
            },
            events: ["*"],
            active:true,
            headers: {
                "X-GitHub-OTP": "two-factor-code"
            }
        }, (err, res) => {
            if (err) {
                reject(err)
                console.log(err)
            } else {
                resolve(res);
            }
        })
    })
})

然后执行:

Promise.all(queue).then((values) => { 
    response.json({message:"OK"}) 
},(err) => {
    response.status(404).json(err)
});