0

I have a call to the function which inturn calls http.get . when the search method is called directly I get the results properly but when I call all the the getAll method throug loop .I am getting empty or undefined results

function search(url, node, notFound) {
return new Promise(function(resolve, reject) {
    http.get(url).then(function(data){

        var val = 1;
        for (var i = 0; i < data.length; i++) {
            $searchModal.find(node).append(`<div class="col-xs-12"><label> ${label} ${val}</label></div>`);
            var val = val + 1;
            for (key in data[i]) {
                var index = 1;
                if (key == "Udfs") {
                    $searchModal.find(node).append(`<div class="col-xs-12"><label>  ${key} </label></div>`);
                    for (udfKey in data[i][key]) {
                        $searchModal.find(node).append(`<div class="col-lg-3" ><div class="input-group"> <span class="input-group-addon" id="key">${udfKey}</span> <input id="accountModelId" type="text" disabled class="form-control" placeholder="" aria-describedby="accountModel" value="${data[i][key][udfKey]}"></div></div>`);
                    }
                }
                for (var j = 0; (Array.isArray(data[i][key])) && j < data[i][key].length; j++) {
                    $searchModal.find(node).append(`<div class="col-xs-12"><label> ${key} ${index}</label></div>`);
                    var index = index + 1;
                    for (objKey in data[i][key][j]) {
                        $searchModal.find(node).append(`<div class="col-lg-3" ><div class="input-group"> <span class="input-group-addon" id="key">${objKey}</span> <input id="accountModelId" type="text" disabled class="form-control" placeholder="" aria-describedby="accountModel" value="${data[i][key][j][objKey]}"></div></div>`);
                    }
                }
                if (!Array.isArray(data[i][key]) && !(data[i][key] instanceof Object)) {
                    $searchModal.find(node).append(`<div class="col-lg-3" ><div class="input-group"> <span class="input-group-addon" id="key">${key}</span> <input id="accountModelId" type="text" disabled class="form-control" placeholder="" aria-describedby="accountModel" value="${data[i][key]}"></div></div>`);
                }
            }
        }
        console.log(data);
        resolve(data.toString());
        if (data.length == 0) {
            $searchModal.find(node).append(notFound);
        }
    }).catch(function err() {
        $searchModal.show("modal")
        $searchModal.find(node).append(notFound);
    });
});

}

 function getAll() {
urls = [contactUrl, accountUrl, assestUrl]
nodes = ['#contactData', '#accountData', '#assetData']
list = [$noContacts, $noAccounts, $noAsset]
for (let i = 0; i < urls.length; i++) {
    search(urls[i], nodes[i], list[i]);
}

}

Need help to handle this .Thanks

Devuatprod
  • 147
  • 1
  • 11
  • Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Jan 07 '19 at 21:40

1 Answers1

2

Two things:

1) You should probably handle rejection when making your request. So in your catch block after you call http.get you should reject(err).

2) The reason you aren't getting a response is because you aren't waiting for one. You are looping through a data set and sending off requests, and then your code just keeps going. What you want to do is wait for all the requests to finish, and then do something. Here is an example using the all() function from es6 Promise library.

 function getAll() {
  urls = [contactUrl, accountUrl, assestUrl]
  nodes = ['#contactData', '#accountData', '#assetData']
  list = [$noContacts, $noAccounts, $noAsset]

  var promiseArray = [];

  for (let i = 0; i < urls.length; i++) {
      promiseArray.push(search(urls[i], nodes[i], list[i]));
  }

  Promise.all(promiseArray)
    .then((resp) => {
      console.log(resp[0]); // response from 1st request
      console.log(resp[1]); // response from 2nd request
    }).catch((err) => {
      console.log(err);
    })
 }
Isaac Vidrine
  • 1,568
  • 1
  • 8
  • 20