Changed promises to use IDs
This commit is contained in:
parent
58a689235a
commit
69edaa2159
1 changed files with 6 additions and 4 deletions
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue