我有每个都有8个项目的数组
var array_pullrequest_id=["335","328","326","323","322","314","295","291"];
var array_uniqueName=["[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]"];
我正在尝试对数组的每个索引进行发布请求:
function test2(){
var array_pullrequest_id=["335","328","326","323","322","314","295","291"];
var array_uniqueName=["[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]"];
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);
});
}
}
现在,这是控制台输出:
first index: 0
first console log pullRequest: 335
first console log Created by: [email protected]
first index: 1
first console log pullRequest: 328
first console log Created by: [email protected]
first index: 2
first console log pullRequest: 326
first console log Created by: [email protected]
first index: 3
first console log pullRequest ID: 323
first console log Created by: [email protected]
first index: 4
first console log pullRequest ID: 322
first console log Created by: [email protected]
first index: 5
first console log pullRequest ID: 314
first console log Created by: [email protected]
first index: 6
first console log pullRequest ID: 295
first console log Created by: [email protected]
first index: 7
first console log pullRequest ID: 291
first console log Created by: [email protected]
second index: 8
second console log pullRequest ID: 291
second console log Created by: [email protected]
second index: 8
second console log pullRequest ID: 291
second console log Created by: [email protected]
second index: 8
second console log pullRequest ID: 291
second console log Created by: [email protected]
second index: 8
second console log pullRequest ID: 291
second console log Created by: [email protected]
second index: 8
second console log pullRequest ID: 291
second console log Created by: [email protected]
second index: 8
second console log pullRequest ID: 291
second console log Created by: [email protected]
second index: 8
second console log pullRequest ID: 291
second console log Created by: [email protected]
second index: 8
second console log pullRequest ID: 291
second console log Created by: [email protected]
现在,您可以看到第一个控制台日志项在count变量索引之后正确打印,但是在for循环请求功能(第二个控制台日志)中,它仅打印最后一个数组项,即使它在循环中,也只是将最后,这对我来说毫无意义...
假设您已经安装了axios,则可以使用async / await,如下所示
注意async
关键字在function test2()
现在test2
是一个异步功能
const axios = require('axios');
async function test2(){
var array_pullrequest_id=["335","328","326","323","322","314","295","291"];
var array_uniqueName=["[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]"];
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);
})
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句