text: app.factory('AwaitService', ['$q', '$rootScope', '$timeout', 'AwaitOptions', function($q, $rootScope, $timeout, AwaitOptions) {
      var service = {};
  
      service.Await = function(eventName, validationCallback) {
          var deferred = $q.defer();
  
          var cancelTimeout = $timeout(function() {
              if (cancelEventListener !== undefined && cancelEventListener !== null) {
                  cancelEventListener();
              }
  
              deferred.reject({
                  code: 2,
                  message: 'Timeout reached'
              });
          }, AwaitOptions.Timeout);
  
          var cancelEventListener = $rootScope.$on(eventName, function() {
              var isValid = false;
  
              if (validationCallback !== undefined && validationCallback !== null) {
                  isValid = validationCallback.apply(null, arguments);
              } else {
                  isValid = true;
              }
  
              if (isValid) {
                  $timeout.cancel(cancelTimeout);
                  deferred.resolve(arguments);
              } else {
                  deferred.reject({
                      code: 1,
                      message: 'Validation not successful'
                  });
              }
          });
  
          return deferred.promise;
      };
  
      return service;
  }]);