O'Reilly logo

Rails, Angular, Postgres, and Bootstrap by David B. Copeland

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Understanding How Asynchronous Requests Work

If you’ve done any JavaScript programming, you are familiar with callbacks. These are functions that get called later. The simplest example is setTimeout, which takes a callback function and a number of milliseconds. After the given milliseconds have elapsed, the function is called.

In this code, we execute the function errorMessage one second later:

 var​ errorMessage = ​function​() {
  alert(​"OH NOES!"​);
 };
 setTimeout(errorMessage,1000);

We also saw this when using $http in Chapter 5, Build a Dynamic UI with AngularJS, where we passed a callback to get that would execute our code once we got a response from the server. We didn’t really talk about why $http works that way. Now we will. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required