Last active
October 2, 2016 21:39
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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