Streamlining Asynchronous Data Handling in Airtel Finance

Bhawna Sharma
Airtel Digital
Published in
8 min readMay 25, 2023

At Airtel Finance, we understand the importance of staying on the cutting edge of web development technologies to provide our customers with the best possible experience. That’s why we’re excited to dive into the world of Async Iterators and Generators in JavaScript. These powerful features can help us handle complex asynchronous data streams more efficiently, allowing us to provide faster, more scalable web applications for our customers.

In this Medium post, we’ll explore how Async Iterators and Generators work and how they can be used in practical applications. We’ll also showcase some practical examples of how they can be used to improve the performance of your web applications. So, whether you’re a seasoned developer or just starting with JavaScript, join us on this journey to discover how Async Iterators and Generators can take your web development skills to the next level.

What are Async Iterators?

At Airtel Finance, we often deal with large or continuous data streams in our web applications, which can be challenging to process efficiently. This is where async iterators can be a valuable tool. By allowing us to process data as it becomes available, we can avoid having to load the entire dataset into memory, which can greatly improve the performance and scalability of our applications.

Async iterators are a type of iterator that can handle asynchronous data streams in JavaScript. They allow iteration over data that is not yet fully available or may take some time to arrive, making them useful for handling large or continuous data streams, such as those provided by web sockets or real-time data feeds.

To use async iterators in JavaScript, you first need to create an asynchronous data source. This can be done using a variety of methods, such as using promises or callbacks to fetch data from an API or streaming data from a web socket. Once you have a data source, you can create an async iterator using the Symbol.asyncIterator method, which is a built-in method in JavaScript.

Here is an example of how to create and use an async iterator in JavaScript:

In this example, we define an async function getData() that uses the yield keyword to return individual data items from the data source. We then create an async iterator using the getData() function and the async function* syntax, and finally, we iterate over the data items using the for await...of loop.

However, it’s important to note that async iterators are not supported in all web browsers, which can pose a challenge for cross-browser compatibility. At Airtel Finance, we take steps to ensure that our applications work seamlessly across a wide range of browsers, including using polyfills or other workarounds as necessary.

Overall, async iterators are a powerful feature of JavaScript that can greatly improve the handling of asynchronous data streams in web applications. At Airtel Finance, we make use of this tool to ensure that our applications are efficient, scalable, and accessible to all users.

What are Generators?

At Airtel, we use JavaScript extensively in our web development projects, and generators are a powerful tool that we utilize for generating sequences of values and controlling the flow of execution in complex applications. With generators, we can write more readable and maintainable code that is easier to understand and reason about, which is essential for developing robust and scalable web applications.

Generators are a feature in JavaScript that allow functions to return an iterable sequence of values, which can be paused and resumed at any point during iteration. This provides a powerful way to generate sequences of values on the fly and to control the flow of execution in complex applications.

In JavaScript, generators are defined using function* syntax, which indicates that the function should behave as a generator. Inside a generator function, the yield keyword is used to indicate a point at which the function should pause and return a value to the caller. The generator can then be resumed at any point by calling its next() method.

Here is an example of a simple generator function in JavaScript:

In this example, we define a generator function myGenerator() that yields three values. We then create a generator using the myGenerator() function and the function* syntax. Finally, we call the next() method on the generator to iterate over the values returned by the generator.

Moreover, generators enable us to write code that is more readable and easier to understand, which is important in large and complex codebases. However, we are also aware of the limitations of using generators, such as their potential complexity and the requirement for support for iterators. Therefore, we carefully weigh the benefits and limitations of using generators in each project to ensure that we are using the appropriate tools to achieve the desired results.

In conclusion, at Airtel Finance, we recognize the power and versatility of generators in JavaScript, and we use them strategically to create efficient and maintainable web applications. By understanding the benefits and limitations of generators, we can make informed decisions about their use in our projects, and deliver high-quality solutions to our customers.

Combining Async Iterators and Generators

At Airtel Finance, we understand the importance of efficiently handling asynchronous data streams in modern JavaScript development. Combining async iterators and generators is a powerful way to achieve this goal. By using iterators to generate data and generators to control the flow of execution, we can create cleaner and more maintainable code that is easier to reason about.

However, we also acknowledge that using async iterators and generators may require more advanced knowledge of JavaScript, and may not be supported in all JavaScript environments. That’s why our team of expert developers and engineers constantly stay up-to-date with the latest advancements in JavaScript technology and make informed decisions about the most appropriate tools and techniques to use.

Here is an example of how to combine async iterators and generators to fetch and process data from an API:

In this example, we define an async iterator getData() that fetches data from an API in pages and returns each page of data as an array. We then define a generator processData() that iterates over the data returned by the getData() iterator, calculates the average value of each item in the data and yields the result.

To combine these two features, we use the for await...of loop to iterate over the values returned by the getData() iterator, and then use a regular for...of loop to iterate over each item in the data.

In conclusion, by taking full advantage of async iterators and generators, we create more powerful and efficient code for handling complex asynchronous data streams at Airtel Finance. We aim to leverage these features to improve the performance and scalability of our web applications while keeping in mind their limitations and ensuring that our developers have the necessary knowledge and expertise to use them effectively.

Real-World Examples

Here are a few real-world examples of how Async Iterators and Generators are used in actual JavaScript code:

  • Streaming Data Processing — Async Iterators and Generators are commonly used for processing large datasets in a streaming fashion. For example, the Node.js fs module provides a createReadStream() method that returns an Async Iterator over the contents of a file. We can then use a for await...of loop to process the data in chunks, without reading the entire file into memory at once. Here's an example:
  • Async Iteration with Third-Party APIs — Async Iterators and Generators are also useful for iterating over data streams provided by third-party APIs that require asynchronous processing. For example, the twitter npm package provides a stream() method that returns an Async Iterator over tweets that match a given search query. We can then use a for await...of loop to iterate over the tweets and process them asynchronously. Here's an example:
  • Async Data Transformation — Async Iterators and Generators can also be used for transforming data streams in an asynchronous way. For example, the pumpify npm package provides a convenient way to chain together multiple Async Iterators to transform data streams. We can use a for await...of loop to consume the transformed data stream asynchronously. Here's an example:

These are just a few examples of using Async Iterators and Generators in real-world JavaScript code. The power and flexibility of these features make them a valuable tool for handling complex asynchronous data streams.

Use in popular libraries & frameworks

Async Iterators and Generators are used extensively in modern JavaScript development, and there are many real-world examples of their use in popular libraries, frameworks, and applications. Here are some examples:

RxJS — It is a popular library for reactive programming in JavaScript, and it makes heavy use of Async Iterators and Generators to handle asynchronous data streams. For example, the from() function in RxJS allows us to create an observable from an Async Iterator, which can be used to consume asynchronous data streams in a more efficient and intuitive way.

In the example, we define an asynchronous generator function getData() that yields three data items. We then use the from() function in RxJS to create an observable from the Async Iterator returned by getData(), which can be subscribed to using the subscribe() method. This allows us to consume the data in a more efficient and intuitive way, without having to deal with the complexities of handling asynchronous data streams manually.

Koa — It is a popular web framework for Node.js, and it makes use of Async Iterators and Generators to handle middleware functions in a more efficient and readable way. For example, the use() method in Koa allows us to define middleware functions using Generators, which can be used to control the flow of execution in complex applications.

In this example, we define two middleware functions using Generators. The first middleware function logs a message before and after the execution of the next middleware function, while the second middleware function logs a message when it is executed. By using Generators to define middleware functions, we can control the flow of execution in a more intuitive and efficient way, and handle complex scenarios more easily.

The Async-Await pattern — It is a popular approach to handling asynchronous code in JavaScript, and it makes use of Async Iterators and Generators to simplify the handling of asynchronous data streams. For example, the following code uses Async-Await to fetch data from an API and log the results:

In this example, we define an asynchronous generator function fetchData() that fetches data from an API using the fetch() function. We then define an asynchronous function getData() that uses the Async-Await pattern to consume the data returned by fetchData() and log the results. By using Async Iterators and Generators together with the Async-Await pattern, we can handle complex asynchronous data streams in a more efficient and intuitive way.

In conclusion, Async Iterators and Generators are powerful features of modern JavaScript and are used extensively in popular libraries, frameworks, and applications.

Conclusion

In summary, at Airtel Finance, we understand the importance of writing clean and efficient code for handling complex asynchronous data streams in JavaScript. This is why we utilize powerful features like Async Iterators and Generators to make our code more intuitive and efficient.

By using Async Iterators, we can iterate over asynchronous data in an efficient and standardized way. We also use Generators to create custom iterators with minimal boilerplate code, resulting in more concise and readable code.

Combining Async Iterators and Generators allows us to handle large or continuous data streams, such as those provided by web sockets or real-time data feeds, in a more efficient and intuitive way. This makes our applications more scalable and capable of processing data as it arrives, without having to wait for the entire dataset to be loaded into memory.

Some useful resources for further reading or learning about Async Iterators and Generators in JavaScript include:

In conclusion, at Airtel Finance, we believe that every modern JavaScript developer should be familiar with Async Iterators and Generators as they are essential tools for handling complex asynchronous data streams and improving the performance and scalability of web applications.

--

--