I dislike JavaScript. Greatly. I think it’s a messy language, though I’ll be the first to admit this may be because I don’t know how to code in it as well as other languages.
I wasted the last three days dealing with bugs that all stemmed from my poor understanding of asynchronous control flow, callback functions and closure.
I’m wondering if there is an easier way to program in JS so that I can return to productivity. My current stack is Flask, MongoDB, JQuery and Bootstrap. Ideally I’d like to augment and not replace those.
What framework or tools would be able to best mitigate the JS issues I outlined above?
I’d suggest you learn how to use jQuery Deferreds. They aren’t required in order to program cleanly with asynchronous functions, but they might be the tool that you’re looking for to provide some direction and structure.
We could offer more specifics if you showed some code examples that seem messy to you.
When I have callback functions that have more than 10-15 lines of code, I break them out into a named function and give it a name that describes what it does. In some cases, I even make it a method on an existing object. For example, in an API, I use that has an ajax all for logging in, I have a method called
ProcessLoginResponse()that processes the login response from the ajax call. If the login succeeds, then this method has to then issue another API call and I create another method on the same object that processes the response to that ajax call to continue the flow. I document with comments how the flow works, but the appropriately named methods make the code seem clean and easy to know where to set breakpoints or make modifications.