2

I have read a few forum posts before on angular promises but can't get it to work in my instance. I am using nodejs /locomotive for the backend and Angular form the frontend.

I have the following code in a controller, basically I want to use the path to slides.path, how would I go about doing this using promises? any help would be gratefully received.

function ProductCtrl($scope, $http, $q) {

    $scope.events = [];
    $scope.times = [];

    var html = [];
    var chapters = [];
    var path;

    //var paPromise = $q.defer();

    $http({
        url: '/show',
        method: 'GET',
        params: { eventid:$scope.$routeParams.eventid}

    }).success(function(response, code) {

        $scope.events = response;

        angular.forEach($scope.events.slides, function(slide) {

            $http({
                url: '/upload',
                method: 'GET',
                params: {uploadid: slide.upload.toString()}

            }).success(function(response, code) {
                return "http://www.example.com/"+response.path;

            },path);

            slide.path = path;

            chapters.push(slide);

        });

    });
}
2
  • 1
    You are doing it with promises. $http returns a promise, and the success call is only invoked when the promise is resolved. What exactly are you having a problem with? Commented Apr 16, 2014 at 2:47
  • Unless you want to go full FRP or enable legacy auto unwinding mode. I'm not sure what you want to accomplish. Commented Apr 16, 2014 at 4:59

1 Answer 1

4

You can use $q.all do get this multiple promise problem done. Like this:

function ProductCtrl($scope, $http, $q) {

$scope.events = [];
$scope.times = [];

var html = [];

var path;

function fetchChapter() {
    var chapters = [];

    var httpPromise = $http({
        url: '/show',
        method: 'GET',
        params: {
            eventid: $scope.$routeParams.eventid
        }
    });

    //Return the value http Promise
    return httpPromise.then(function (response) {
        $scope.events = response;
        var promises = [];
        angular.forEach($scope.events.slides, function (slide) {

            var inPromise = $http({
                url: '/upload',
                method: 'GET',
                params: {
                    uploadid: slide.upload.toString()
                }
            }).then(function (response, code) {
                //each promise makes sure, that he pushes the data into the chapters
                slide.path = "http://www.example.com/" + response.path;
                chapters.push(slide);
            });
            //Push the promise into an array
            promises.push(inPromise);
        });
        //return the promise from the $q.all, that makes sure, that all pushed promises are ready and return the chapters.
        return $q.all(promises).then(function () {
            return chapters;
        });
    });
}

fetchChapter().then(function(chapters){
   //populate here 
});

}

The httpPromise will return the promise from $q.all.

EDIT: How to fetch the data then Wrap a function around, i did with fetchChapter and pass a function into the then there will be the value you need as a parameter.

Sign up to request clarification or add additional context in comments.

4 Comments

Eh. why not return chapter from the promise? You can simply map the slices to promises on them.
the "s" was missing ^^ chapters.push(slide)
thanks for the reply how would I use the var chapters to populate a div outside the httpromise
That did the trick and I have began to understand promises, cool, cheers

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.