Sample Video Frame

Created by Zed A. Shaw Updated 2024-02-17 04:54:36

30: Callbacks, Events, Promises and Async

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'll then talk about each one and why that style doesn't actually work. The reason I'm doing this is because--even though you should use async/await in all new code, you'll constantly run into older libraries that still use one of these styles.

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 course.

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.

Previous Lesson Next Lesson

Register for Learn JavaScript the Hard Way

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.