When you’re learning any new programming tool or technique it’s helpful to have examples of how to use the code in realistic situations, so in addition to covering the fundamentals we have a series of posts showing practical demonstrations of how to use asynchronous code.
You’ll learn a couple of important things from this post: 1) how to create an Observable wrapper around an API request so your code gets a notification when the …
await keywords; and it even includes RxJS Observables, a widely-used external library for handling asynchronous tasks.
Each of the posts includes complete source code for a runnable Node.js project demonstrating the technique covered in the post, and the posts are unified by a common case study so you can compare different approaches to the same task. The code for each post …
Often it doesn’t matter when a Promise returns data and the members of a collection of Promises can be resolved independently without regard to timing. A function can perform asynchronous tasks that don’t depend on the data from, or the success of, other asynchronous tasks.
But sometimes business rules or program design require that asynchronous actions are dependent on other asynchronous operations. For example:
- Establishing a remote database connection must be preceded by loading configuration information from the file system.
- Performing a REST API call to a weather API must be preceded by obtaining the user’s location from …
Understanding the case study project
In this post you will see how …
Introducing the ReactiveX API and the RxJS library
Reactive Extensions, known as ReactiveX, is an application programming interface (API) enabling programming languages …
You can put these skills together to retrieve remote data through an HTTP request and work with the call response asynchronously. But in doing so you’ll probably notice that code has at least one pitfall: the logical order of declared functions is reversed. That makes the code hard to read and maintain.
Promises can also be chained together to use the state of settled promises (resolved or rejected) and the promises’ values to control the flow of execution in your program. This is useful when you have a sequence of asynchronous operations to perform that each depend on the results of the previous operation: the next step in the chain isn’t performed until the preceding step is resolved. Chains of promises also include error propagation; errors in a …
Promises offer a number of advantages:
- a promise can be called multiple times
- promises can be chained together
- promises can include error rejection events
- errors in a chain of promises need only be handled once (error propagation)
- promises can be used to wrap old-style callback functionality
- promises always execute after the current execution stack is empty