0

I tried the $q service as follows but the browser still not waiting for the response. I already spent almost a day to figure out a solution, I also tried the $timeout.

login.controller("loginCtrl", ['$scope', '$rootScope', '$filter',   '$window', '$http', 'APIService', 'localStorageService', 'doAlert', '$translate', '$q', '$timeout',

  function ($scope, $rootScope, $filter, $window, $http, APIService, localStorageService, doAlert, $translate, $q, $timeout) {
    $scope.isLogOut = true;

    $(window).unload(function () {
        $rootScope.$broadcast('onUnload');
    });
    $scope.$on('onUnload', function (e) {
        var deferred = $q.defer();
        $http.get(url).
                success(function (data, status, headers, config) {
                    deferred.resolve(data);
                }).
                error(function (data, status, headers, config) {
                    deferred.reject(status);
                });
        return deferred.promise;
    });
Pardeep Dhingra
  • 3,916
  • 7
  • 30
  • 56
user2617611
  • 339
  • 1
  • 4
  • 16
  • Just sayin: `$http`already returns a promise. So there shouldn't be a need of `$q`. – Aer0 Nov 08 '16 at 08:35
  • tried this also . But no luck $http.get(url).success(function(response){ response.data; }); – user2617611 Nov 08 '16 at 09:28
  • Use `$http.then(function(success) { ... }, function(error) { ... })` instead of success and error. You may also take a look at the docs: https://docs.angularjs.org/api/ng/service/$http – Aer0 Nov 08 '16 at 10:26
  • Even with this code, browser does not wait for the response, before it closes. – user2617611 Nov 08 '16 at 17:51
  • Try this: https://stackoverflow.com/questions/20184070/angularjs-have-method-return-synchronously-when-it-calls-http-or-resource-int – Aer0 Nov 08 '16 at 18:36

1 Answers1

0

Here is a sample where I use a callback to process data back from my utility

function get(url, callback, obj) {
    $http.get(url, obj)
    .then(function (response) {
        callback(response.data);
    }, function () {
        notification.error(PLEASE_CONTACT_SYS_ADMIN);
    });
}
Barak
  • 535
  • 6
  • 18
  • I am getting angular.min.js:81 ReferenceError: callback is not defined. Can you please elaborate a little bit with my Angular $scope.$on('onUnload') event handler. Kind of new to this. – user2617611 Nov 08 '16 at 09:19