O'Reilly logo

Rails, Angular, Postgres, and Bootstrap, 2nd Edition 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 Asynchronous Requests and Observables

If you’ve done any JavaScript programming, you are familiar with callbacks. These are functions that get called after some work has completed. 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, the function errorMessage is executed one second later:

 var​ greeting = ​function​() {
  alert(​"Hello!"​);
 };
 setTimeout(greeting,1000);

You first saw this when using Http in Chapter 6, Build a Dynamic UI with Angular, when you passed a callback to subscribe that would execute our code once we got a response from the server. I didn’t really talk about why Http works ...

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