0

Service call in for loop angular js $q, promise

   var FULLWEEKDAYS = [MONDAY, TUESDAY ... SATURDAY]
    for (var i=0; i< FULLWEEKDAYS.length; i++) {
                var reqParams = {
                    weekday: FULLWEEKDAYS[i],
                    teacherId : 97
                }
                TimetableService.getTeachersOccupancy(reqParams, function (data) 
    {
                    if (data) {
                        $scope.weeklyData.push(data);
                    }
                }, function (err) {
                    //message.error('Timetable', err.data);
                });
            }

Serivice call is

function getTeachersOccupancy(data, successFunction, errorFunction) {            
var params = $.param(data);
        AjaxHandlerFactory.AjaxGet(BASETIMETABLEPATH + 'occupancy?' + 
params, {}, function (response) {
            successFunction(response.data);
        }, function (error) {
            errorFunction(error);
        });
    }

Question: $scope.weeklyData.length = 0 outside for loop. Why and how to handle this in promises?

Ganesh Manickam
  • 2,113
  • 3
  • 20
  • 28
  • Possible duplicate of [Angularjs $q.all](https://stackoverflow.com/questions/21310964/angularjs-q-all) – Kaustubh Khare Dec 08 '17 at 05:22
  • 1
    Why is explained here in detail: [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – charlietfl Dec 08 '17 at 05:24
  • Best to use `Promise.all(FULLWEEKDAYS.map(weekday=>promise)` If you would like to learn more about promises, how they work and why you need them there is some information here: https://stackoverflow.com/a/47678417/1641941 A full example of Promise.all is in this answer: https://stackoverflow.com/a/43766002/1641941 – HMR Dec 08 '17 at 06:17
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – HMR Dec 08 '17 at 06:20
  • HMR: I didnt get you. Can you please provide code snippet. Im new to angularJS. – Sri Lalitha Dec 12 '17 at 06:38

1 Answers1

0

Serivce call

function getTeachersOccupancy(data, successFunction, errorFunction) {
        // /SchoolAdminWS/services/schools/{schoolCd}/timeTable/occupancy?classroomId={classroomId}&date={YYYY-MM-DD}
        var params = $.param(data);
        ***var deferred = $q.defer();***
        AjaxHandlerFactory.AjaxGet(BASETIMETABLEPATH + 'occupancy?' + params, {}, function (response) {
            successFunction(response.data);
            ***deferred.resolve(response.data);***
        }, function (error) {
            errorFunction(error);
            ***deferred.reject(error);***
        });
        ***return deferred.promise;***
    }

While calling above service, create a variable promise=[]; push all repsonses from service call, and resolve them.

var promises = [];
        for (var i=0; i< FULLWEEKDAYS.length; i++) {
            var reqParams = {
                weekday: FULLWEEKDAYS[i],
                teacherId : vm.employeeProfileId
            }
            var promise = TimetableService.getTeachersOccupancy(reqParams, function () {}, function () {});
            promises.push(promise);
        }

Now resolve using $q.all()

$q.all(promises).then(function(value) {
            vm.weeklyData = value;
            console.log(vm.weeklyData);
            setTeacherOccupancyData(value);
            vm.isSearch = true;
        }, function (reason) {
            console.log("Promise Rejected:" + reason);
        });