Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何解析多个请求以发送javascript响应?_Javascript_Angularjs_Node.js_Callback_Synchronized - Fatal编程技术网

如何解析多个请求以发送javascript响应?

如何解析多个请求以发送javascript响应?,javascript,angularjs,node.js,callback,synchronized,Javascript,Angularjs,Node.js,Callback,Synchronized,我需要使用不同的参数(node.js和express.js)对外部api执行相同的请求 如果对我的请求的所有回答都是正确的,那么我将发送angular客户端代码200,但是,如果有任何错误,请向他发送错误。 我尝试过类似的方法,但不起作用: for(var i = 0; i<array.length; i++){ github.repos.createHook({ user: user, repo: arra

我需要使用不同的参数(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"});

                        }


                    }

for(var i=0;i试试这个:

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

你可以使用承诺,这是一个干净而简单的方法

首先创建一个包含承诺的队列:

   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)
});
  • 这个例子只是解释了如何使用承诺,代码还没有经过测试
  • 还可以查看以下方面的文档:

i
永远不要转到array.length,试试
i==array.length-1
createHook
是同步的吗?不是NODE.js的家伙,但我想你可以在后端使用某种deffer机制轻松做到这一点。如果你没事,你可以在前端(角度)完全处理这个问题通过链接http promisesNo,它是异步的,我认为I==array.length-1不起作用,因为循环在它成为请求之前就结束了。我不确定。我如何连接这样的承诺?然后(函数(结果){defered.resolve(结果);},函数(err){defered.reject(err);});return promise;使用诸如who handle Parallelism之类的工具非常感谢,我会尝试:d此方法有时会返回超时错误,其他人告诉我,使用eachseries()方法可能更好,因为我想在发送其他请求之前等待外部服务器的响应。您认为如何?