DEV Community

Cover image for Demystifying Advanced Asynchronous Patterns: Generators, Iterators, and RxJS
Best Codes
Best Codes

Posted on

Demystifying Advanced Asynchronous Patterns: Generators, Iterators, and RxJS

Demystifying advanced asynchronous patterns: Going beyond Promise.all and async/await and exploring cutting-edge techniques like generators, async iterators, and reactive programming with RxJS.

Asynchronous programming is a powerful way to write code that can handle multiple tasks at the same time, without blocking the main thread. However, it can also be challenging to manage complex flows of data and events, especially when dealing with errors, cancellations, and concurrency issues.

In this post, I will show you some of the advanced techniques that you can use to handle asynchronous patterns in JavaScript, beyond the basic Promise.all and async/await syntax. These techniques include:

  • Generators: A special kind of function that can pause and resume its execution, and yield multiple values over time.
  • Async iterators: A way to iterate over asynchronous data sources, such as streams, files, or web sockets, using the for await...of loop.
  • Reactive programming with RxJS: A paradigm that treats data and events as observable streams, and allows you to compose and transform them using declarative operators.

By the end of this post, you will have a better understanding of these techniques, and how to use them in your own projects. Let's get started!

Generators

Generators are a special kind of function that can pause and resume its execution, and yield multiple values over time. You can create a generator function by using the function* keyword, and use the yield keyword to return a value and pause the function. To resume the function, you need to call the next() method on the generator object, which returns an object with two properties: value and done. The value property contains the yielded value, and the done property indicates whether the generator has finished or not.

Here is a simple example of a generator function that yields the numbers from 1 to 10:

function* count() {
  let n = 1;
  while (n <= 10) {
    yield n;
    n++;
  }
}

// Create a generator object
const gen = count();

// Call the next() method to get the next value
console.log(gen.next()); // { value: 1, done: false }
console.log(gen.next()); // { value: 2, done: false }
console.log(gen.next()); // { value: 3, done: false }
// ...
console.log(gen.next()); // { value: 10, done: false }
console.log(gen.next()); // { value: undefined, done: true }
Enter fullscreen mode Exit fullscreen mode

One of the benefits of generators is that they allow you to create lazy sequences of data, that are only computed when needed. This can save memory and improve performance, especially when dealing with large or infinite data sets.

Another benefit of generators is that they can be used to model asynchronous operations, such as fetching data from an API, or reading data from a file. To do this, you need to use the yield keyword to pause the generator until the asynchronous operation is resolved, and then resume it with the result. However, this requires some extra logic to handle the promises and errors, which can make the code more verbose and complex.

For example, here is how you can use a generator to fetch data from an API, and handle errors:

function* fetchUsers() {
  try {
    // Pause the generator and wait for the promise to resolve
    const response = yield fetch('https://jsonplaceholder.typicode.com/users');
    // Resume the generator with the response
    const users = yield response.json();
    // Return the users
    return users;
  } catch (error) {
    // Handle the error
    console.error(error);
  }
}

// Create a generator object
const gen = fetchUsers();

// Call the next() method with a dummy value to start the generator
gen.next().value
  .then((response) => {
    // Call the next() method with the response to resume the generator
    return gen.next(response).value;
  })
  .then((users) => {
    // Call the next() method with the users to finish the generator
    console.log(gen.next(users)); // { value: users, done: true }
  })
  .catch((error) => {
    // Handle the error
    console.error(error);
  });
Enter fullscreen mode Exit fullscreen mode

As you can see, this code is not very elegant, and it requires a lot of boilerplate code to handle the promises and errors. Fortunately, there is a better way to use generators for asynchronous operations, and that is by using async iterators.

Async iterators

Async iterators are a way to iterate over asynchronous data sources, such as streams, files, or web sockets, using the for await...of loop. The for await...of loop is similar to the regular for...of loop, but it can handle promises and await for them to resolve before moving to the next iteration.
To create an async iterator, you need to use the async function* keyword, and use the yield keyword to return a promise. The for await...of loop will automatically call the next() method on the async iterator, and await for the promise to resolve. If the promise is rejected, the loop will throw an error and exit.

Here is how you can rewrite the previous example using an async iterator:

async function* fetchUsers() {
  try {
    // Yield a promise and wait for it to resolve
    const response = yield fetch('https://jsonplaceholder.typicode.com/users');
    // Yield another promise and wait for it to resolve
    const users = yield response.json();
    // Return the users
    return users;
  } catch (error) {
    // Handle the error
    console.error(error);
  }
}

// Create an async iterator object
const gen = fetchUsers();

// Use the for await...of loop to iterate over the async iterator
(async () => {
  for await (const users of gen) {
    // Log the users
    console.log(users);
  }
})();
Enter fullscreen mode Exit fullscreen mode

As you can see, this code is much more concise and readable, and it handles the promises and errors automatically. The for await...of loop is a great way to work with async iterators, and it can also be used with other built-in async iterables, such as ReadableStream, Blob, or File.

However, there is one limitation of async iterators, and that is that they are pull-based, meaning that you have to explicitly request the next value from the iterator. This can be problematic when dealing with push-based data sources, such as web sockets, timers, or user events, that emit data at their own pace, and may not wait for you to consume them. In this case, you need a way to handle the data as it arrives, and that is where reactive programming with RxJS comes in.

Reactive programming with RxJS

Reactive programming is a paradigm that treats data and events as observable streams, and allows you to compose and transform them using declarative operators. RxJS is a library that implements reactive programming in JavaScript, and provides a rich set of tools to work with asynchronous data.

An observable is a data source that can emit zero or more values over time, and can be subscribed to by one or more observers. An observer is an object that defines three methods: next, error, and complete, that are called when the observable emits a value, throws an error, or completes. An operator is a function that takes an observable as input, and returns a new observable as output, applying some transformation or logic along the way.
Here is a simple example of creating an observable, applying an operator, and subscribing to it:

// Import RxJS
import { of, map } from 'rxjs';

// Create an observable that emits the numbers from 1 to 10
const source$ = of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

// Apply an operator that multiplies each value by 2
const double$ = source$.pipe(map((x) => x * 2));

// Subscribe to the observable and log the values
double$.subscribe({
  next: (value) => console.log(value),
  error: (error) => console.error(error),
  complete: () => console.log('Done'),
});

// Output:
// 2
// 4
// 6
// 8
// 10
// 12
// 14
// 16
// 18
// 20
// Done
Enter fullscreen mode Exit fullscreen mode

One of the benefits of reactive programming with RxJS is that it allows you to handle both pull-based and push-based data sources in a uniform way, using observables. For example, you can create an observable from a web socket, a timer, or a user event, and apply the same operators to them as you would to an array or a promise.

Another benefit of reactive programming with RxJS is that it allows you to compose and transform multiple observables using a variety of operators, such as filter, map, reduce, merge, concat, switchMap, debounce, throttle, and many more. These operators enable you to express complex asynchronous logic in a declarative and concise way, without having to deal with callbacks, promises, or async/await.

For example, here is how you can use RxJS to fetch data from an API, and handle errors, retries, and cancellations:

// Import RxJS
import { from, of, throwError, timer, switchMap, catchError, retryWhen, takeUntil } from 'rxjs';
import { ajax } from 'rxjs/ajax';

// Create an observable that fetches data from an API
const fetch$ = from(ajax.getJSON('https://jsonplaceholder.typicode.com/users'));

// Create an observable that emits a cancel signal after 10 seconds
const cancel$ = timer(10000);

// Apply operators to handle errors, retries, and cancellations
const result$ = fetch$.pipe(
  // If the fetch fails, throw an error
  catchError((error) => throwError(error)),
  // If the error is a network error, retry up to 3 times with a delay of 1 second
  retryWhen((errors) =>
    errors.pipe(
      switchMap((error) => {
        if (error.name === 'AjaxError') {
          return timer(1000);
        } else {
          return throwError(error);
        }
      }),
      take(3)
    )
  ),
  // If the cancel signal is emitted, abort the fetch
  takeUntil(cancel$)
);

// Subscribe to the observable and log the results
result$.subscribe({
  next: (users) => console.log(users),
  error: (error) => console.error(error),
  complete: () => console.log('Done'),
});
Enter fullscreen mode Exit fullscreen mode

As you can see, this code is much more expressive and flexible, and it handles the errors, retries, and cancellations automatically. The pipe method is a great way to compose and transform observables, and it can also be used with other built-in observables, such as timer, interval, or fromEvent.

However, there is one challenge of reactive programming with RxJS, and that is that it requires a different way of thinking, compared to the traditional imperative or async/await style. You have to learn how to use observables, observers, operators, and schedulers, and how to deal with concepts such as multicasting, unsubscription, and backpressure. In this case, you need a lot of practice and patience to master reactive programming with RxJS.

Conclusion

In this post, we have seen how to use generators, async iterators, and reactive programming with RxJS to handle advanced asynchronous patterns in JavaScript. These techniques allow us to write code that can handle multiple tasks at the same time, without blocking the main thread, and with less boilerplate and complexity.

Generators are a special kind of function that can pause and resume its execution, and yield multiple values over time. They can be used to create lazy sequences of data, and to model asynchronous operations.

Async iterators are a way to iterate over asynchronous data sources, such as streams, files, or web sockets, using the for await...of loop. They can handle promises and errors automatically, and make the code more concise and readable.

Reactive programming with RxJS is a paradigm that treats data and events as observable streams, and allows us to compose and transform them using declarative operators. It can handle both pull-based and push-based data sources, and express complex asynchronous logic in a uniform way.

I hope you enjoyed this post, and learned something new and useful. If you want to learn more about these topics, here are some resources that I recommend:

Thank you for reading, and happy coding! 😊

Feedback

What do you think of this post? Do you have any questions or suggestions? Please leave a comment below, and I will try to reply as soon as possible. I appreciate your feedback and support. 👍


Best_codes
https://the-best-codes.github.io/
Some content and / or codes in this post were AI generated, reworded, or fixed. Best_codes is not responsible for non-functional codes, loss of property, or personal dissatisfaction with content.

Top comments (7)

Collapse
 
kshyun28 profile image
Jasper Gabriel

The topics flow nicely from each other. :)

These are definitely advanced concepts that I haven't used myself. How often do you find using these patterns in your workloads?

I feel when you're at a point of optimizing for performance/memory, these can help.

Collapse
 
best_codes profile image
Best Codes

Thanks, I'm glad the article flows!

I'd have to say, I don't use these very often, but sometimes I just do, I guess?? It just kind of happens unintentionally.

These techniques certainly can be useful for memory conversation or performance.

Thanks for the feedback!

Collapse
 
kshyun28 profile image
Jasper Gabriel

I see. They would be a nice addition to my tools, and they do help with specific problems. :)

Collapse
 
thecheapaudiophile profile image
Griff Polk

Did michael tell you about the pronouns? That, i respect.

Collapse
 
best_codes profile image
Best Codes

Yes, he did. Did he tell you the same?

Collapse
 
thecheapaudiophile profile image
Griff Polk

Yeah, i respect that. I was totally fine with it.

Thread Thread
 
best_codes profile image
Best Codes

Me as well. 😊 Have a great week!