client-ui
Version:
Testing implementation of nodeJs Backend, angular frontend, and hopefully in a way that this can be deployed to s3/cloudfront
59 lines (52 loc) • 1.79 kB
JavaScript
(function () {
'use strict';
angular.module("clientApp").factory("promiseWrapper", ["$q", "$timeout", function($q, $timeout) {
var defaultTimeout = 30000;
var promiseFn = function(qDep) {
if (typeof qDep === "undefined") {
this.deferred = $q.defer();
} else {
this.deferred = qDep.defer();
}
this.resolved = false;
this.promise = this.deferred.promise;
this.timeout = false;
};
promiseFn.prototype.timeoutAfter = function(params) {
if (typeof params === "undefined") {
params = {};
}
if (typeof params.timeout === "undefined") {
params.timeout = defaultTimeout;
}
if (!this.resolved) {
this.cancelTimeout();
this.timeout = $timeout(function() {
this.timeout = false;
this.reject(params.error);
}.bind(this), params.timeout);
}
};
promiseFn.prototype.resolve = function(value) {
this.cancelTimeout();
if (!this.resolved) {
this.resolved = true;
this.deferred.resolve(value);
}
};
promiseFn.prototype.reject = function(value) {
this.cancelTimeout();
if (!this.resolved) {
this.resolved = true;
this.deferred.reject(value);
}
};
promiseFn.prototype.cancelTimeout = function() {
if (this.timeout !== false) {
$timeout.cancel(this.timeout);
this.timeout = false;
}
};
return promiseFn;
}]);
})();