Javascript 循环内部的nodejs请求函数

Javascript 循环内部的nodejs请求函数,javascript,node.js,api,https,request,Javascript,Node.js,Api,Https,Request,我有一个数组,每个数组有8个项目 var array_pullrequest_id=["335","328","326","323","322","314","295","291"]; var array_uniqueName=["A@A.com","B@B.com","C@C.com&q

我有一个数组,每个数组有8个项目

var array_pullrequest_id=["335","328","326","323","322","314","295","291"];
var array_uniqueName=["A@A.com","B@B.com","C@C.com","D@D.com","E@E.com","F@F.com","G@G.com","H@H.com"];
我尝试对数组的每个索引执行post请求:

 function test2(){
   
    var array_pullrequest_id=["335","328","326","323","322","314","295","291"];
    var array_uniqueName=["A@A.com","B@B.com","C@C.com","D@D.com","E@E.com","F@F.com","G@G.com","H@H.com"];
    var count = 8;
     for (var i=0; i<count; i++){
     
    var pullRequests_id = array_pullrequest_id[i];    
    var createdBy = array_uniqueName[i];
  
  console.log("first index: " + i);
  console.log("first console log pullRequest ID: " + pullRequests_id);
  console.log("first console log Created by: " + createdBy);

  var options = {
  'method': 'GET',
  'url': 'https://HIDEN_URL/pullRequests/'+ pullRequests_id+'/workitems',
  'headers': {
    'Authorization': 'Basic HIDEN_AUTH',
    'Cookie': 'HIDEN_COOKIE'
  }
}
request(options, function (error, response) { 
    console.log("second index: " + i);
    console.log("second console log pullRequest ID: " + pullRequests_id);
    console.log("second console log Created by: " + createdBy);

});
}
 }

现在您可以看到,第一个控制台日志项在count变量索引后正确打印,但在for循环请求函数中(第二个控制台日志)只打印最后一个数组项,即使它在循环中,也只打印最后一个,这对我来说毫无意义…

第二个控制台日志之所以只打印数组中的最后一项,是因为它在
请求
中,这是一个
异步
函数,这意味着当
request
函数在后台运行以返回承诺时,请求函数之外的其余代码将正常运行,因此循环将不断迭代,直到发出服务请求的数组中的最后一项本质上是阻塞的,所以我们应该使用回调或承诺来获得输出你期待着。尝试使用asyncForEach,这样您就可以实现上述输出

async function test2() {

    var array_pullrequest_id = ["335", "328", "326", "323", "322", "314", "295", "291"];
    var array_uniqueName = ["A@A.com", "B@B.com", "C@C.com", "D@D.com", "E@E.com", "F@F.com", "G@G.com", "H@H.com"];
    var count = 8;

    await asyncForEach(array_pullrequest_id, async(pullrequest, index) => {
        var pullRequests_id = array_pullrequest_id[index];
        var createdBy = array_uniqueName[index];

        console.log("first index: " + index);
        console.log("first console log pullRequest ID: " + pullRequests_id);
        console.log("first console log Created by: " + createdBy);

        var options = {
            'method': 'GET',
            'url': 'https://HIDEN_URL/pullRequests/' + pullRequests_id + '/workitems',
            'headers': {
                'Authorization': 'Basic HIDEN_AUTH',
                'Cookie': 'HIDEN_COOKIE'
            }
        }
        request(options, function(error, response) {
            console.log(error);
            console.log("second index: " + index);
            console.log("second console log pullRequest ID: " + pullRequests_id);
            console.log("second console log Created by: " + createdBy);

        });
    });
}

async function asyncForEach(array, callback) {
    for (let index = 0; index < array.length; index++) {
        await callback(array[index], index, array);
    }
}
异步函数test2(){ var数组_pullrequest_id=[“335”、“328”、“326”、“323”、“322”、“314”、“295”、“291”]; 变量数组_uniqueName=[”A@A.com", "B@B.com", "C@C.com", "D@D.com", "E@E.com", "F@F.com", "G@G.com", "H@H.com"]; var计数=8; 等待asyncForEach(数组\u pullrequest\u id,异步(pullrequest,索引)=>{ var pullRequests\u id=array\u pullRequests\u id[index]; var createdBy=array_uniqueName[index]; log(“第一个索引:”+索引); log(“第一个控制台日志pullRequest ID:+pullRequests\u ID”); log(“创建的第一个控制台日志:”+createdBy); 变量选项={ '方法':'获取', “url”:”https://HIDEN_URL/pullRequests/“+pullRequests_id+”/workitems”, “标题”:{ “授权”:“基本隐藏授权”, “Cookie”:“HIDEN_Cookie” } } 请求(选项、功能(错误、响应){ console.log(错误); 日志(“第二个索引:”+索引); log(“第二个控制台日志pullRequest ID:+pullRequests\u ID”); log(“创建的第二个控制台日志:”+createdBy); }); }); } 异步函数asyncForEach(数组、回调){ for(让index=0;index您还可以使用async for loop“如果您的节点版本支持它”,如上所述,您的代码的问题是请求本质上是异步的。

假设您已经安装了axios,您可以使用async/Wait,如下所示

注意
函数test2()前面的
async
关键字

现在,
test2
是一个异步函数

const axios = require('axios');

async function test2(){
   
    var array_pullrequest_id=["335","328","326","323","322","314","295","291"];
    var array_uniqueName=["A@A.com","B@B.com","C@C.com","D@D.com","E@E.com","F@F.com","G@G.com","H@H.com"];
    var count = 8;
    for (var i=0; i<count; i++){

        var pullRequests_id = array_pullrequest_id[i];    
        var createdBy = array_uniqueName[i];

        console.log("first index: " + i);
        console.log("first console log pullRequest ID: " + pullRequests_id);
        console.log("first console log Created by: " + createdBy);

        var options = {
        'method': 'GET',
        'url': 'https://HIDEN_URL/pullRequests/'+ pullRequests_id+'/workitems',
        'headers': {
        'Authorization': 'Basic HIDEN_AUTH',
        'Cookie': 'HIDEN_COOKIE'
            }
        }
        await axios(options)
        .then(() => {
            console.log("second index: " + i);
            console.log("second console log pullRequest ID: " + pullRequests_id);
            console.log("second console log Created by: " + createdBy);
        })
        
    }
 }
const axios=require('axios');
异步函数test2(){
var数组_pullrequest_id=[“335”、“328”、“326”、“323”、“322”、“314”、“295”、“291”];
变量数组_uniqueName=[”A@A.com","B@B.com","C@C.com","D@D.com","E@E.com","F@F.com","G@G.com","H@H.com"];
var计数=8;
对于(var i=0;i{
console.log(“第二个索引:+i”);
log(“第二个控制台日志pullRequest ID:+pullRequests\u ID”);
log(“创建的第二个控制台日志:”+createdBy);
})
}
}

我认为请求是一个异步函数,因此第一个控制台日志和第二个控制台日志不是按顺序执行的。循环首先完成,然后请求函数将获得在循环结束时设置的最后一个变量值,这就是第二个控制台日志打印291和H2H的原因。com@grandia您建议我在非异步的NodeJ中使用什么其他方式来执行http请求?我建议使用其他框架,因为请求似乎已被弃用。我用过axios和fetch,我认为两者都很棒。为了使其同步,可以使用async/await。你熟悉async/await吗?谢谢你的建议我不熟悉async/await,但我会尝试改用这些框架:)!当然,让我为它贴一个代码
const axios = require('axios');

async function test2(){
   
    var array_pullrequest_id=["335","328","326","323","322","314","295","291"];
    var array_uniqueName=["A@A.com","B@B.com","C@C.com","D@D.com","E@E.com","F@F.com","G@G.com","H@H.com"];
    var count = 8;
    for (var i=0; i<count; i++){

        var pullRequests_id = array_pullrequest_id[i];    
        var createdBy = array_uniqueName[i];

        console.log("first index: " + i);
        console.log("first console log pullRequest ID: " + pullRequests_id);
        console.log("first console log Created by: " + createdBy);

        var options = {
        'method': 'GET',
        'url': 'https://HIDEN_URL/pullRequests/'+ pullRequests_id+'/workitems',
        'headers': {
        'Authorization': 'Basic HIDEN_AUTH',
        'Cookie': 'HIDEN_COOKIE'
            }
        }
        await axios(options)
        .then(() => {
            console.log("second index: " + i);
            console.log("second console log pullRequest ID: " + pullRequests_id);
            console.log("second console log Created by: " + createdBy);
        })
        
    }
 }