0

I am calling this method testDataService, which is defined in the same JS file. This method returns a list which it gets from an API call. I want to set that value to model and return model object. but my return function is getting called before my API returns the response. How can i make sure return function will get called only after the above if block get the data from API.

       getTestData(model) {

            model.name = "practice";
            model.value = 2;

    // this if loop is calling an API and setting up data into model.

           if(this.model.form.length>0 && this.model.form[0].entityName === 'test'){
           let responseList;
           this.testDataService(this.model).then(response => {
           responseList = response;
           model.class = responseList;

    });
    }

           return this.model;

       // this return function is getting called before the api returns its data. How can i make sure this return will get called only after the above if block get the data from API.

    }



    This is method definition

        testDataService(model) {
          let profileType = "test";
          let profileId = "test_profile";
          let configCategory = "test";
          return this.referenceDataService.getTestData(profileType, profileId, configCategory);
        }
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – 31piy Jun 01 '18 at 08:39
  • you need to return a Promise, which `$http` service already does. Simple example: `return $http.get(url,data).then((res)=>{return res.data;})`. So don't return some model, but a Promise instead. Then once you receive it elsewhere, you would need to resolve it with: `this.retrieveTestData(this.model).then((res) => {let responseList = res;})` – Aleksey Solovey Jun 01 '18 at 08:45
  • what does this function is doing : this.testDataService.getClearingListData(profileType, profileId, configCategory). Code is not very much clear. create a plunker or explain more about your code, – Kusum Kushwaha Jun 01 '18 at 08:54
  • Your function will always return newModel because the call above is async and JS runs on a single thread, therefore it'll continue executing after calling your API. – Protozoid Jun 01 '18 at 09:23

1 Answers1

1

You need to pass return statement from the callback, instead of placing it outside.

getTestData(model) {
  model.name = "practice";
  model.value = 2;

 // this if loop is calling an API and setting up data into model.

 if (this.model.form.length > 0 && this.model.form[0].entityName === 'test') {
    this.testDataService(this.model).then(response => {
        let responseList = response;
        model.class = responseList;
        return this.model;
    }).then(response => {
        return null;
    });
  }
}

testDataService(model) {
 let profileType = "test";
 let profileId = "test_profile";
 let configCategory = "test";
 return this.referenceDataService.getTestData(profileType, profileId, configCategory);
}
Jay
  • 338
  • 1
  • 5