How do you track and visualize JavaScript Promises?

When writing a fairly large JavaScript module with a lot of asynchronous operation dealing with promises, it is quite difficult to debug and monitor the flow / state of promises.

Are there any tools or frameworks for testing tests or module code to give it a stream of all Promises in my system? I use jQuery Promises, if that matters at all.

+8
javascript jquery promise testing
source share
2 answers

Beyond the top of my head, and driven by @MrLeap's idea of ​​sending messages to the console , how about creating a Pending adapter for your own design.

for example (embryonic and unverified):

 var debugMode = true; function DeferredAdapter(name) { var dfrd = $.Deferred(); if(debugMode) { dfrd.notify = function() { console.log(name + ': notify'); if (arguments[0] && typeof arguments[0] == "string") console.log(arguments[0]); }; //similar for resolve //similar for reject } return dfrd; } 

That way (when you have it working), you can do unnecessary things when debugging, with a simple mechanism to turn off unnecessary material in the production code without having to go through the entire console.log() cleansing statement.

+2
source share

This will add quite a bit of development overhead, but if you use node for the back end, you can implement pretty reliable asynchronous unit tests with vows.js. If you want to add even more work for yourself, you can use the oaths to create mock versions of all your answers, if you use any other server technician and use it for testing and reporting. Using node.js mock-server also allows you to create reliable enterprise ^^ objects, for example, simulate certain requests, without seeing how elegantly your interface responds to it.

I wish I had a better answer than that; console.log and console.dir are your best friends. Fill your callbacks full of them so you can understand how long the material has been going on and in what order they have been going.

+1
source share

All Articles