Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
Q promise tracker
var noop = function(){};
// remove existing Q (hopefully it has no state...)
delete require.cache[require.resolve('q')];
// remove freezing so we can modify Q
var origFreeze = Object.freeze;
Object.freeze = noop;
// require Q and restore freezing
var Q = require('q');
Object.freeze = origFreeze;
// copy/pasted from Q - we'll defend it after we modify it
var defend = Object.freeze || noop;
if (typeof cajaVM !== "undefined") {
defend = cajaVM.def;
var current = [], prev = [], total=0;
// add the promise to an array
// make sure to remove it when it is done
// oh, and "defend" it...
var recordPromise = function(promise)
var rm = function()
// not the most efficient way... but who cares
current = current.filter(function(i){ return i!=promise; });
var nextPromise = promise.then(rm, rm);
return nextPromise;
// modify makePromise and defer - I think these are the only places
// that create promises
var origMake = Q.makePromise;
Q.makePromise = function()
return recordPromise(origMake.apply(this, arguments));
var origDefer = Q.defer;
Q.defer = function()
var deferred = origDefer.apply(this, arguments);
deferred.promise = recordPromise(deferred.promise);
return deferred;
// defend things that should've been defended...
// logs:
// * how many are still unresolved
// * how many of those were in the previous snapshot and are still unresolved
// * how many new promises were created since last snapshot
exports.snapshot = function() {
var now = new Date().getTime();
var stillCount = current.filter(function(p){ return prev.indexOf(p)>=0; }).length;
console.log("Not kept:",current.length,"From before:",stillCount,"Generated:",total);
prev = current.slice();
total = 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.