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);
        }
4
  • Possible duplicate of How do I return the response from an asynchronous call? Commented Jun 1, 2018 at 8: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;}) Commented Jun 1, 2018 at 8: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, Commented Jun 1, 2018 at 8: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. Commented Jun 1, 2018 at 9:23

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Nice one, I was gonna suggest exactly the same
@PriyankaTaneja Updated solution. Hope it will help!
@Jay Thanks alot

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.