1

I want to construct an array with the help of 3 other arrays. I am using JS promise for this purpose but not able to get the implementation right. SO what I basically want to do is to populate an array first.

let getPlacePromise = function() {
      return new Promise(function(resolve,reject){
        this.getplaceArray= this.getfilterPlaces(inputdata);
        resolve("got places\n" ); 
      });
  };

  let getTransporterPromise= function(message) {
    return new Promise(function(resolve,reject){
      this.getTransporterArray= this.getfilterTransporter(inputdata);
      resolve(message+"got Transporter"); 
    });
  }; 

  let getVehiclePromise = function(message) {
    return new Promise(function(resolve,reject){
      this.getVehicleArray= this.getfilterVehicles(inputdata);
      resolve(message+"got vehicle"); 
    });
  };

  getPlacePromise().then(function(result){
      return getTransporterPromise(result);
  }).then(function(result){
      return getVehiclePromise(result);
  }).then(function(result){
    this.AllDropdownValues= this.getTransporterArray.concat(this.getVehicleArray);
  });

I know this code should have errors. I am not able to come up with the right implementation. I want getplaceArray to populate first than only the other other arrays should be complete. Is it possible to call function inside promises. The error I was getting here was getplaceArray, getTransporterArray, getVehicleArray are undefined.

getfilterTransporter function:

getfilterTransporter(autocompleteInputData) {

var k= this.checkRegex(autocompleteInputData);
this.getfilteredTransporter= this.filterTransporters(k);
return this.formatTransporterValue(this.getfilteredTransporter);

}

getfilterVehicle :

getfilterVehicles(autocompleteInputData) {

  var k= this.checkRegex(autocompleteInputData);
    this.getfilteredVehicle= this.filterVehicles(k);
    return this.formatVehicleValue(this.getfilteredVehicle);
}

getfilterPlace

getfilterPlaces(autocompleteInputData) {
  if (autocompleteInputData == '' || typeof(autocompleteInputData) == 'object') 
  return null;
  this.placeData.getPlacesFromPig(autocompleteInputData)
  .subscribe(response => {
     return this.formatPigResponse(response);
   }); 

}

Edit :

Modified the answers a bit. Rest are working but this.getfilterPlaces which has a subscribe method running inside it to populate data is not working.

let a=[], b=[],c=[];
    a=  this.getfilterPlaces(inputdata);
    b= this.getfilterTransporter(inputdata);
    c= this.getfilterVehicles(inputdata);

    let getplaceArray = [],
  getTransporterArray = [],
  getVehicleArray = [];

let getPlacePromise = function () {
  const self = this;
  return new Promise(function (resolve, reject) {
    getplaceArray = a;
    resolve("got places\n");
  });
};

let getTransporterPromise = function (message) {
  const self = this;
  return new Promise(function (resolve, reject) {
    getTransporterArray =  b
    resolve(message + "got Transporter");
  });
};

let getVehiclePromise = function (message) {
  const self = this;
  return new Promise(function (resolve, reject) {
    getVehicleArray = c
    resolve(message + "got vehicle");
  });
};

getPlacePromise().then(function (result) {
  return getTransporterPromise(result);
}).then(function (result) {
  return getVehiclePromise(result);
}).then(function (result) {
  var AllDropdownValues = getTransporterArray.concat(getVehicleArray).concat(getplaceArray);
  console.log(AllDropdownValues);
});

The answer is use of async and await.Below is the code.

async function getfilterPlaces(autocompleteInputData) {
  if (autocompleteInputData == '' || typeof(autocompleteInputData) == 'object') 
    return null;
  return this.placeData.getPlacesFromPig(autocompleteInputData)
    .toPromise()
    .then(response => this.formatPigResponse(response));
}

while calling the function filterAllComponent(inputdata) :

async function filterAllComponent(inputdata) {
  let a=[], b=[], c=[];
  a = await this.getfilterPlaces(inputdata);
  b = this.getfilterTransporter(inputdata);
  c = this.getfilterVehicles(inputdata);
[...]
Udit Gogoi
  • 675
  • 2
  • 11
  • 28

1 Answers1

0

try this

let getplaceArray = [],
  getTransporterArray = [],
  getVehicleArray = [];

let getPlacePromise = function () {
  const self = this;
  return new Promise(function (resolve, reject) {
    getplaceArray = self.getfilterPlaces(inputdata);
    resolve("got places\n");
  });
};

let getTransporterPromise = function (message) {
  const self = this;
  return new Promise(function (resolve, reject) {
    getTransporterArray = self.getfilterTransporter(inputdata);
    resolve(message + "got Transporter");
  });
};

let getVehiclePromise = function (message) {
  const self = this;
  return new Promise(function (resolve, reject) {
    getVehicleArray = self.getfilterVehicles(inputdata);
    resolve(message + "got vehicle");
  });
};

getPlacePromise().then(function (result) {
  return getTransporterPromise(result);
}).then(function (result) {
  return getVehiclePromise(result);
}).then(function (result) {
  this.AllDropdownValues = getTransporterArray.concat(getVehicleArray);
});
Rahul Sharma
  • 9,534
  • 1
  • 15
  • 37