Last Updated: February 25, 2016
·
1.949K
· jkring

Debugging Async Javascript

There is a bit of a black hole in javascript rendered asynchronously by rails. Let's say you have something like this:

$.post("/users/index", { authenticity_token:"<%= form_authenticity_token %>"}, null, "script");

which asynchronously loads a bunch of users into your browser. Any javascript bug in that index.js.erb that you are rendering is elusive. It doesn't throw an exception in your javascript console (because it's asynchronous), and it obviously doesn't throw a rails exception, because rails doesn't even know it's buggy.

An easy way to debug is to navigate to localhost:3000/users/index?format=js which will render all the buggy javascript. You can then copy that into your javascript console in the window which called the aforementioned $.post. Run it and voila! The bug is not so elusive anymore.