johnburnsonline.com

Mastering the Yield* Keyword in JavaScript Generators

Written on

Chapter 1: Understanding Yield*

In JavaScript, the yield* keyword serves as a unique syntax that permits delegation to another generator or iterable object. This functionality allows a generator to yield values from another generator, array, or any iterable, effectively merging their outputs into a single cohesive sequence. In this concise, hands-on guide, we will explore the practical applications of yield* within JavaScript generators.

For those looking to quickly familiarize themselves with JS generators, consider checking out the following resource: Using yield* with Generators.

Section 1.1: Yielding from Another Generator

When utilizing yield* with an additional generator, the current generator yields all values from the delegated generator until it completes. This feature is particularly advantageous for decomposing intricate generator functions into simpler, modular components.

function* numberGenerator() {

yield 1;

yield 2;

yield 3;

}

function* combinedGenerator() {

yield* numberGenerator();

yield* ['a', 'b', 'c'];

}

const combined = combinedGenerator();

for (let value of combined) {

console.log(value);

}

In this illustration, the combinedGenerator function employs yield* to yield values from both the numberGenerator and an array.

The first video dives into generators, function*, yield, and yield* in a professional setting, providing a comprehensive understanding of these concepts.

Section 1.2: Yielding from Iterables

The yield* keyword can also be utilized to iterate directly over any iterable object, such as arrays or strings, within a generator.

function* stringGenerator() {

yield* "Hello";

}

const stringGen = stringGenerator();

for (let char of stringGen) {

console.log(char);

}

In this scenario, yield* iterates over and yields each character of a string.

The second video provides a quick tutorial on learning JavaScript generators in just 12 minutes, perfect for those short on time.

Chapter 2: The Power of Nested Generators

The yield* keyword proves especially powerful when used with nested generators, facilitating complex, multi-level iterations.

function* nestedGenerator() {

yield* numberGenerator();

yield 'Middle';

yield* stringGenerator();

}

const nested = nestedGenerator();

for (let value of nested) {

console.log(value);

}

In this nested example, yield* efficiently combines the outputs from multiple generators and an iterable.

Conclusion

In summary, we have explored how yield* enhances the versatility and functionality of generators, promoting modular code and efficient iteration over different types of iterables.

For those interested in combining generators with promises, check out this detailed article: In Plain English 🚀.

Thank you for being a part of the In Plain English community! Before you leave, don't forget to clap and follow the author! ️👏️️

Follow us on: X | LinkedIn | YouTube | Discord | Newsletter

Explore our other platforms: Stackademic | CoFeed | Venture

Find more content at PlainEnglish.io

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Launching the Future: How Artemis 1 Sets the Stage for Lunar Exploration

Artemis 1 is a pivotal mission that redefines lunar exploration, paving the way for future human endeavors on the moon and beyond.

Fastest Asteroid Discovered: A Glimpse into the Cosmos

Astronomers have identified the fastest asteroid, 2021 PH27, which orbits the sun every 113 days while maintaining a safe distance from Earth.

Insights on Tiago Forte’s New Book: The PARA Method Explained

A concise review of Tiago Forte's latest book, exploring its insights and limitations in the context of digital organization strategies.

Navigating the Complexities of Modern Medical Marketing

An exploration of how modern medical marketing shapes patient perceptions and choices, emphasizing the need for informed decision-making.

Understanding Cryptocurrency Prices: Trends and Insights

Explore the current state of cryptocurrency prices as of January 2023 and learn about the top players in the market.

Embrace Your Aspirations Through Patience and Resolve

Discover how patience and determination can help you achieve your goals, inspired by the process of making fudge frosting.

# Evaluating the Cost of Upgrading to the Mac mini M2 Pro

This article discusses the pros and cons of upgrading to the Apple Mac mini with M2 Pro from a senior software engineer's perspective.

The Possibility of Cryogenic Freezing: A Reality Check

This article explores the intriguing concept of cryogenic freezing, its potential, and the challenges that remain.