Convert promise in JSON object

VitezKoja picture VitezKoja · Jun 2, 2017 · Viewed 35.6k times · Source

I have a problem converting the promise returned by the service to the controller. What I want is to create an array of JSON objects from the data contained in promise. Here is what I receive in the controller: Data received at controller

Here is the line that is used for printing that data:

console.log("controller unmatched: ", unmatched.getData(sFilter));

Here "unmatched" is the service, and getData() is its function.

Thanks!

Answer

Lennholm picture Lennholm · Jun 2, 2017

A Promise represents a value that will be available some time in the future, or never. This means its eventual value can't be returned by your unmatched.getData() function.

What you need to do is to make unmatched.getData() return the actual promise and then you take action when that promise resolves:

unmatched.getData(sFilter).then(function(result) {
  console.log("controller unmatched: ", result);
});