0

What am i trying to achieve is as such:

  • Invoking my service to retrieve all appointments in appointment types (number of types not fixed) tied to a doctor
  • If there are 3 appointment types, then there will be 3 async calls made
  • return a single promise with $q.all() after all 3 promises have been resolved

appointmentService

this.getAllDoctorAppointments = function (doctor, appointmentTypeArray) {

        var promises = [];

        angular.forEach(appointmentTypeArray, function (appointmentType) {

            var defer = $q.defer();

            $http.get('/appointments/?doctorName=' + doctor + '&apptType=' + appointmentType)
                .success(function (listOfAppointments) {

                    defer.resolve(listOfAppointments);
                    promises.push(defer.promise);

                });
        });

        return $q.all(promises);

    };

In my console log, the appointmentType returns [ ]. This happens because the empty 'promises' array is returned even before the 3 async calls are made. I am still very new to the concept of promises, what is the best approach to work this out? Thanks!

$scope.getAllDoctorAppointments = function (doctor, appointmentTypeArray) {

    appointmentService.getAllDoctorAppointments(doctor, appointmentTypeArray)

        .then(function (appointmentType) {

//could take in any number. hardcoded 3 just for testing.
console.log(appointmentType)

            angular.forEach(appointmentType[0], function (xRay) {
                $scope.xRayAppts.events.push(xRay);
            });

            angular.forEach(appointmentType[1], function (ctScan) {
                $scope.ctScanAppts.events.push(ctScan);
            });

            angular.forEach(appointmentType[2], function (mri) {
                $scope.mriAppts.events.push(mri);
            });

        });

};
1
  • don't need the var defer but at the same time it would work if you didn't push the promise inside the success callback...too late then .. push it outside of success Commented Oct 14, 2015 at 20:00

2 Answers 2

1
this.getAllDoctorAppointments = function (doctor, appointmentTypeArray) {
    var promises = [];

    angular.forEach(appointmentTypeArray, function (appointmentType) {

        promises.push($http.get('/appointments/?doctorName=' + doctor + '&apptType=' + appointmentType)
            .success(function (listOfAppointments) {
                return listOfAppointments;
            });
        );
    });

    return $q.all(promises);
};

$http.get returns the promises that you wants to collect, there is no need for a new defer in this case.

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

Comments

0

the promise is not being added to the array because the code that adds it to the array, promises.push(defer.promise);, is inside of the result code of the thing you are trying to defer. So the promise wouldn't get added to the list of promises to execute until after it executed!

so you can either move that push line outside of the success call looking something like this:

angular.forEach(appointmentTypeArray, function (appointmentType) {

        var defer = $q.defer();

        $http.get('/appointments/?doctorName=' + doctor + '&apptType=' + appointmentType)
            .success(function (listOfAppointments) {

                defer.resolve(listOfAppointments);

            });
        promises.push(defer.promise);

    });

or, you can do as lcycool suggests and just add the $http.get(...).success(...) calls to the array directly.

Comments

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.