Skip to content

Instantly share code, notes, and snippets.

@Stuk
Last active October 2, 2016 21:39
Show Gist options
  • Save Stuk/5827281 to your computer and use it in GitHub Desktop.
Save Stuk/5827281 to your computer and use it in GitHub Desktop.
I've found this code useful for debugging promises, to find what order things are happening in. `console.error` gives a stack trace in the Webkit Developer Tools which makes easily narrow down where the promise creation and resolution is happening.
var Q = require("q");
var Set = require("collections/set"); // https://npmjs.org/package/collections
window.outstandingPromises = new Set();
var originalDefer = Q.defer;
Q.defer = function () {
console.error("Deferred created");
var deferred = originalDefer();
deferred.stack = new Error("").stack;
window.outstandingPromises.add(deferred);
var originalResolve = deferred.resolve;
deferred.resolve = function (value) {
console.error("Promise resolved with", value);
if (deferred.promise.isPending()) window.outstandingPromises.delete(deferred);
originalResolve(value);
};
var originalReject = deferred.reject;
deferred.reject = function (value) {
console.error("Promise rejected with", value);
if (deferred.promise.isPending()) window.outstandingPromises.delete(deferred);
originalReject(value);
};
return deferred;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment