D
D
Denis Denis2016-01-18 09:43:57
JavaScript
Denis Denis, 2016-01-18 09:43:57

How to return a result from a promise?

Hello!
There is an Angular service

taskApp.service('taskService', ['$http',
    function ($http) {

        this.getTaskList = function () {

          $http.get("tasks.json").then(function (response) {
              return response.data; //приходит undefined в контроллер, НИПАНЯТНА :)
            }).catch(function (error) {
                console.log(error.status);
            });
        };
    }
]);

I can not figure out how to return the result from the service method.
If you return all this call to $http, then a promise is returned, but you just want an
array
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nicholas, 2016-01-18
@golovewkin

Well, you are in the place where you call this method, process the promise:

service.getTasksList().then( function(data) { controller.tasks = data; });

E
Evgeny Kumanin, 2016-01-18
@jackkum

return $http.get("tasks.json").then(function(response){
  return response.data;
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question