public
Created

Cancellable deferreds

  • Download Gist
cancellable-deferreds.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
var deferredsByPromise = new WeakMap();
 
recordRetriever.retrieveAsync = function (query) {
var deferred = Q.defer();
 
server.doPagedRetrieveWithCallback(query, 0, function (err, result) {
// wire up deferred.resolve and deferred.reject.
// recurse appropriately, increasing pagination,
// UNLESS deferred.isCancelled.
});
 
deferredsByPromise.set(deferred.promise, deferred);
return deferred.promise;
};
 
recordRetriever.cancel = function (promise) {
var deferred = deferredsByPromise.get(promise);
deferred.cancel();
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.