0

I have to call multiple API using promise all in for loop but I am getting a response in unsequential format. e.g. I have 3 rows but getting a response in 1,2,3 sequence. First getting the first-row response than 3rd row and then getting 2nd row but I need to get a response in a sequential format like (1 row,2 rows,3 rows).

result = '{"error_code":0,"err_desc":null,"data":[{"":"","name":"OTT, KATHRYN M","address":"3110 Horseshoe Trl, Glenmoore, PA","email":"","phone1":"(410) 599-2212","phone2":"(610) 827-9107","phone3":"(610) 308-4566","phone4":"(610) 506-1121","phone5":"(610) 469-0737","phone6":"(610) 942-4347","phone7":"323-7898","phone8":"(814) 371-6133","phone9":""},{"":"","name":"BELTRANTE, SUSAN E","address":"3 Rhoads Ave, Moorestown, NJ\"","email":"[email protected], [email protected],","phone1":"(856) 266-0381","phone2":"(856) 273-0869","phone3":"(609) 266-0381","phone4":"(856) 235-3933","phone5":"","phone6":"","phone7":"","phone8":"","phone9":""},{"":"","name":"Manish","address":"4895 E American Beauty Dr, Tucson, AZ 85756","email":"[email protected]","phone1":"(857) 266-0381","phone2":"(857) 273-0869","phone3":"(610) 266-0381","phone4":"(857) 235-3933","phone5":"","phone6":"","phone7":"","phone8":"","phone9":""}]}';

var i;
                for (i = 0; i < result.length; i++) 
                //for (i = 0; i <= 0; i++) 
                { 
                    var phone = result[i].phone9;
                    var name = result[i].name;
                    var address = result[i].address;
                    var email = result[i].email;
                    var phone1 = result[i].phone1;
                    var phone2 = result[i].phone2;
                    var phone3 = result[i].phone3;
                    var phone4 = result[i].phone4;
                    var phone5 = result[i].phone5;
                    var phone6 = result[i].phone6;
                    var phone7 = result[i].phone7;
                    var phone8 = result[i].phone8;
                    
                    var addressinfo = address.split(',');
                    var street = addressinfo[0];
                    var city = addressinfo[1];
                    var state = addressinfo[2];
                    var zip = addressinfo[3];
                    
                    Promise.all([
                        fetch('https://backend.mioym.properties/api/247/eppraisal?street='+street+'&zip='+zip),
                        fetch('https://backend.mioym.properties/api/247/zillow?street='+street+'&zip='+zip),
                        fetch('https://backend.mioym.properties/api/247/pennymac?address='+address),
                        fetch('https://backend.mioym.properties/api/247/chase?address='+address),
                        fetch('https://backend.mioym.properties/api/247/realtor?address='+address)

                    ]).then(function (responses) {
                        // Get a JSON object from each of the responses
                        return Promise.all(responses.map(function (response) {
                            console.log("here"+i);
                            //console.log(response.json());
                            console.log(response.url);
                            return response.json();
                        }));
                    }).then(function (data) {
                        console.log("success"+i);
                        // Log the data to the console
                        // You would do something with both sets of data here
                        console.log(data);
                    }).catch(function (error) {
                        console.log("error"+i);
                        // if there's an error, log it
                        console.log(error);
                    });
                }                

So please anyone suggest me solution.

2
  • How did you check First getting the first-row response than 3rd row and then getting 2nd row Commented Nov 5, 2020 at 11:21
  • @AbishekKumar I am getting a response in function (data) function and I check response with manually call API response. Commented Nov 5, 2020 at 11:36

1 Answer 1

1

The second Promise.all inside your then block is not necessary, as responses will already contain the resolved values. Note that Promise.all processes the requests in parallel but the resolved responses will be in order. So you can simply do:

Promise.all([
        fetch('https://backend.mioym.properties/api/247/eppraisal?street=' + street + '&zip=' + zip),
        fetch('https://backend.mioym.properties/api/247/zillow?street=' + street + '&zip=' + zip),
        fetch('https://backend.mioym.properties/api/247/pennymac?address=' + address),
        fetch('https://backend.mioym.properties/api/247/chase?address=' + address),
        fetch('https://backend.mioym.properties/api/247/realtor?address=' + address)

    ]).then(function (responses) {
    // Get a JSON object from each of the responses
      return responses.map(function (response) {            
        console.log(response.url);
        return response.json();
      });
});
Sign up to request clarification or add additional context in comments.

1 Comment

What do you mean? responses is an array that contains the resolved responses, you then map this array to get the json response payload for each element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.