Changed promises to use IDs

This commit is contained in:
Marcin Kurczewski 2014-10-04 13:15:18 +02:00
parent 58a689235a
commit 69edaa2159

View file

@ -3,26 +3,28 @@ var App = App || {};
App.Promise = function(_, jQuery) { App.Promise = function(_, jQuery) {
var active = []; var active = [];
var promiseId = 0;
function make(callback) { function make(callback) {
var deferred = jQuery.Deferred(); var deferred = jQuery.Deferred();
var promise = deferred.promise(); var promise = deferred.promise();
promise.promiseId = ++ promiseId;
callback(function() { callback(function() {
deferred.resolve.apply(deferred, arguments); deferred.resolve.apply(deferred, arguments);
active = _.without(active, promise); active = _.without(active, promise.promiseId);
}, function() { }, function() {
deferred.reject.apply(deferred, arguments); deferred.reject.apply(deferred, arguments);
active = _.without(active, promise); active = _.without(active, promise.promiseId);
}); });
promise.then(function() { promise.then(function() {
if (!_.contains(active, promise)) { if (!_.contains(active, promise.promiseId)) {
throw new Error('Broken promise'); throw new Error('Broken promise');
} }
}); });
active.push(promise); active.push(promise.promiseId);
return promise; return promise;
} }