JavaScript, TypeScript, Promises, and callback hell

I’m working on a little project right now, where I’m pushing at the edges of my limited JavaScript skills. I’ve written a fair amount of JavaScript code in the past, but it’s mostly been simple DOM manipulation stuff with jQuery, and/or fairly straightforward Ajax calls. This new project requires a lot of nested calls to async functions that might succeed or might fail, and the whole thing is getting pretty confusing. Luckily, it’s not a high-priority project, so I’ve got time to mess around and try new things with it.

To start with, I’ve switched from plain JavaScript to TypeScript. There’s only a little bit of a learning curve there, since TypeScript is a superset of JavaScript. And I’m picking up TypeScript as I go. I’ve got one Packt ebook: Learning TypeScript, which was their free ebook of the day recently. It’s a bit out of date, but it’s a good start. I’ve bookmarked a couple of PluralSight videos on TypeScript too, but haven’t had time to watch them. I don’t like to add a new language into the mix for a real work project without careful consideration, but TypeScript seems to be relatively low-risk. It’s got some momentum right now, it’s got Microsoft behind it, and if it fails, I can always throw out the .ts files, work from the generated .js files, and pretend TypeScript never happened. (Not that I think that’s at all likely.)

I’ve found myself falling into “callback hell” on this project, so this video called “Redemption from Callback Hell” caught my attention. I’d already discovered promises, but I’ve been using the jQuery implementation, which apparently isn’t a great one. I guess I need to look into Q and the Promises/A+ spec. I’ve read a number of blog posts and articles about promises, but I’m still having some issues with figuring out how to handle some stuff with them.

I’m trying to avoid going down too many rabbit holes on this project, but going down a few is unavoidable. One of the reasons I was using jQuery promises is that I already had jQuery in the project and didn’t want to add another library. But I guess I’ll have to consider at least one more.

Leave a Reply