You may already know that JavaScript is implemented as a single-threaded event loop, making it synchronous. Thus, no two script codes can run in parallel. Angular applications can make use of a Promise, which is part of ECMAScript 6, or Observables from RxJS, for handling asynchronous data. In the near future, Observables may also be made part of standard JavaScript.
A Promise allows you to define handlers for an asynchronous event's eventual completion. We used a Promise in Chapter 10, Angular Forms, too, for performing asynchronous validation. Here's a sample snippet for using a Promise:
return new Promise( (resolve, reject) => {
if (/* some condition */) {
resolve("success");
}
reject("failed");
});
The function passed to a promise is an executor function that would perform...