30: Callbacks, Events, Promises and Async

This exercise is very large and you must watch the video to really understand it. I am going to introduce you to all the various ways that JavaScript handles events using callbacks, promises, and a new thing called a async/await. This exercise is long because I am going to actually build a version of a simple function to read a file in each style. I am then going to critique and re-factor each code sample in the video so you can see how I would redo this or simply avoid the example.

I'm going to be honest with you and say that JavaScript is absolutely insane when it comes to handling computation with callbacks and events. Node.JS famously declared that events were the easiest way to perform computations and then promptly decided that every computation would be handled with callbacks. This misunderstanding of event-based processing meant that they stumbled around for years trying to come up with various solutions to the problem of handling I/O. This means whenever you see code you are going to run into as many as four styles of programming depending on how old the code is.

What I'm hoping to do is show you all the different styles you might run into, show you the problems with each style, and show you how to work around the problems or simply avoid them. This will make the exercise large but it's a good final exercise for the first half of the book. Take your time, watch the videos, and really study the code so that you understand how this is done.

Finally, I am purposefully choosing a particular API that is difficult to use because it demonstrates this flaw in JavaScript and Node.JS. I believe that most people would simply avoid this API and use any of the others, but this kind of problem comes up often enough that you would need to know how to handle this.

Learn JavaScript Today

Register today for the Beginner Basics course and get the new releases by just paying the difference. There's also special discounts for existing customers when new courses are released.


Still Not Sure? Check out more curriculum!