Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Cancellable deferreds
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment