Javascript Loop through array and make call API for each value in array - Stack Overflow

Which javascript loop would be best for the below?I am wanting to make API calls for each value in the

Which javascript loop would be best for the below?

I am wanting to make API calls for each value in the array. Values could number more than what is shown below. I've looked through the loop types and also the promise all, I'm confused which to do Example

//    API Call 1. ABC
//    API Call 2. DEF
//    API Call 3. GHI
//    API Call ....

// Input format example [ABC, DEF, GHI, ... ...]
    
    var alp = "ABC, DEF, GHI";
    var letters = alp.split(',');
    var array = [letters];

    
    // API Request
    
    var apiRequest = http.request({
        'endpoint': 'site',
        'path':'/api/test/table/records', 
        'method': 'POST',
        "headers": {
        "Authorization": "Basic xxxxxxxxxxx=",
        "Content-Type": "application/json"
    
        }
    
    });
    
    
    var data = {};
    
    var dept = {};
    
    // Switch naming
    
    switch (letters[0]) {
    
        case 'ABC':
            site = "A B.C";
            break;
        case 'DEF':
            site = "D E.F";
            break;
        case 'GHI':
            site = "G H.I";
            break;
    }
    
    var u_department = dept;
    data.u_department = u_department;
    
    var apiResponse = apiRequest.write(data);

Where do I place this section

var data = {};
var site = {};

   switch (letters[0]) {
    
        case 'ABC':
            site = "A B.C";
            break;
        case 'DEF':
            site = "D E.F";
            break;
        case 'GHI':
            site = "G H.I";
            break;
    }
var u_department = site;
data.u_department = u_department;
var apiResponse = apiRequest.write(data);

Which javascript loop would be best for the below?

I am wanting to make API calls for each value in the array. Values could number more than what is shown below. I've looked through the loop types and also the promise all, I'm confused which to do Example

//    API Call 1. ABC
//    API Call 2. DEF
//    API Call 3. GHI
//    API Call ....

// Input format example [ABC, DEF, GHI, ... ...]
    
    var alp = "ABC, DEF, GHI";
    var letters = alp.split(',');
    var array = [letters];

    
    // API Request
    
    var apiRequest = http.request({
        'endpoint': 'site',
        'path':'/api/test/table/records', 
        'method': 'POST',
        "headers": {
        "Authorization": "Basic xxxxxxxxxxx=",
        "Content-Type": "application/json"
    
        }
    
    });
    
    
    var data = {};
    
    var dept = {};
    
    // Switch naming
    
    switch (letters[0]) {
    
        case 'ABC':
            site = "A B.C";
            break;
        case 'DEF':
            site = "D E.F";
            break;
        case 'GHI':
            site = "G H.I";
            break;
    }
    
    var u_department = dept;
    data.u_department = u_department;
    
    var apiResponse = apiRequest.write(data);

Where do I place this section

var data = {};
var site = {};

   switch (letters[0]) {
    
        case 'ABC':
            site = "A B.C";
            break;
        case 'DEF':
            site = "D E.F";
            break;
        case 'GHI':
            site = "G H.I";
            break;
    }
var u_department = site;
data.u_department = u_department;
var apiResponse = apiRequest.write(data);
Share Improve this question edited Aug 12, 2020 at 5:58 user3236169 asked Aug 12, 2020 at 4:42 user3236169user3236169 1552 gold badges3 silver badges13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Best and simplest would be to use a for loop and store each promise in an array. Once loop finishes you can use Promise.all(promiseArray) to perform actions based on resolved/rejected promises.

let promiseArray = [];
for(let i=0;i<data.length;i++){
   var apiRequest = http.request({
       ....
     }
    });
   promiseArray.push(apiRequest)
}

Promise.all(promiseArray)
.then(fn)
.catch(fn)

You can read more about Promise.all([])

https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

Here is a small example using JSONPlaceholder APIs

const todos = [1,2,3,4,5];
let promiseArray = [];
for(let i=0;i<todos.length;i++){
 promiseArray.push(fetch('https://jsonplaceholder.typicode./todos/'+todos[i]))
}

Promise.all(promiseArray)
.then(values=>values.map(value=>console.log(value.url+" ==> "+value.status)))
.catch(err=>console.log(err))

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745231801a4617709.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信