avatarParthipan Natkunam

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2843

Abstract

ber">2</span>; <span class="hljs-keyword">yield</span> <span class="hljs-number">3</span>; }

<span class="hljs-keyword">function</span>* <span class="hljs-title function_">combinedGenerator</span>(<span class="hljs-params"></span>) { <span class="hljs-keyword">yield</span>* <span class="hljs-title function_">numberGenerator</span>(); <span class="hljs-keyword">yield</span>* [<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>, <span class="hljs-string">'c'</span>]; }

<span class="hljs-keyword">const</span> combined = <span class="hljs-title function_">combinedGenerator</span>();

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> value <span class="hljs-keyword">of</span> combined) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value); }</pre></div><p id="53b2">In this example, <code>combinedGenerator</code> uses <code>yield*</code> to yield values from both <code>numberGenerator</code> and an array.</p><figure id="0cb1"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*x7wV_KdatzbDw7zWZhaycQ.png"><figcaption>Output of the code execution</figcaption></figure><h1 id="7ddf">Using yield* with Iterables</h1><p id="68ff"><code>yield*</code> can also be used to iterate over any iterable object (like arrays or strings) directly within a generator.</p><div id="917f"><pre><span class="hljs-keyword">function</span>* <span class="hljs-title function_">stringGenerator</span>(<span class="hljs-params"></span>) { <span class="hljs-keyword">yield</span>* <span class="hljs-string">"Hello"</span>; }

<span class="hljs-keyword">const</span> stringGen = <span class="hljs-title function_">stringGenerator</span>();

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> char <span class="hljs-keyword">of</span> stringGen) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(char); }</pre></div><p id="4dcf">Here, <code>yield*</code> is used to iterate over and yield each character of a string.</p><figure id="b2ec"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*FY9_JxORB2cFTURIbbP1LA.png"><figcaption>Output of the code snippet for yield* with Iterables</figcaption></figure><h1 id="9f43">Nested Generators</h1><p id="71bf"><code>yield*</code> can be especially powerful when used with nested generators, allowing for complex, multi-level iterations.</p><div id="74ac"><pre><span class="hljs-keyword">function</span>* <span class="hljs-title function_">nestedGenerator</span>(<span class="hljs-params"></span>) { <span class="hljs-keyword">yield</span>* <span class="hljs-title function_">numberGenerator</span>(); <span class="hljs-keyword">yield</span> <span class="hljs-string">'Middl

Options

e'</span>; <span class="hljs-keyword">yield</span>* <span class="hljs-title function_">stringGenerator</span>(); }

<span class="hljs-keyword">const</span> nested = <span class="hljs-title function_">nestedGenerator</span>();

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> value <span class="hljs-keyword">of</span> nested) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value); }</pre></div><p id="b285">In this nested example <code>yield*</code> seamlessly combines the outputs of multiple generators and an iterable.</p><figure id="88e9"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*GA-X06HLnenK2VAz6bzbCQ.png"><figcaption>Output for the code snippet of using yield* with nested generators</figcaption></figure><h1 id="f984">Conclusion</h1><p id="7580">Thus we have quickly looked at how <code>yield*</code> enhances the functionality and flexibility of generators, allowing for more modular code and easy iteration over various types of iterables.</p><p id="beaa">If you’d like to understand how we could use generators and promises in combination, this detailed article will be of use:</p><div id="f234" class="link-block"> <a href="https://levelup.gitconnected.com/mastering-asynchronous-javascript-leveraging-generators-with-promises-566907c9832e"> <div> <div> <h2>Mastering Asynchronous JavaScript: Leveraging Generators with Promises</h2> <div><h3>Combining Generators and Promises in JavaScript</h3></div> <div><p>levelup.gitconnected.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*2kL-kN7kQ0nyXyXTzIXHAg.png)"></div> </div> </div> </a> </div><h1 id="1a3b">In Plain English 🚀</h1><p id="2066"><i>Thank you for being a part of the <a href="https://plainenglish.io"><b>In Plain English</b></a> community! Before you go:</i></p><ul><li>Be sure to <b>clap</b> and <b>follow</b> the writer ️👏<b>️️</b></li><li>Follow us: <a href="https://twitter.com/inPlainEngHQ"><b>X</b></a><b> | <a href="https://www.linkedin.com/company/inplainenglish/">LinkedIn</a> | <a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw">YouTube</a> | <a href="https://discord.gg/in-plain-english-709094664682340443">Discord</a> | <a href="https://newsletter.plainenglish.io/">Newsletter</a></b></li><li>Visit our other platforms: <a href="https://stackademic.com/"><b>Stackademic</b></a><b> | <a href="https://cofeed.app/">CoFeed</a> | <a href="https://venturemagazine.net/">Venture</a></b></li><li>More content at <a href="https://plainenglish.io"><b>PlainEnglish.io</b></a></li></ul></article></body>

The Yield* Keyword in JS Generators

A Guide to Delegating Yields to Another Generator

Yield* Keyword in JS Generators

In JavaScript generators, yield* is a special syntax used to delegate to another generator or iterable object. It allows a generator to yield the values from another generator, an array, or any other iterable object, effectively flattening their outputs into a single sequence.

In this brief, yet hands-on article, let’s look at how it can be used in our JavaScript generators.

If you’d like to quickly get up to speed on JS generators, feel free to have a look at the following articles:

Using yield* with Generators

When yield* is used with another generator, the current generator will yield all values from the delegated generator until that generator is done. This is particularly useful for breaking complex 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 example, combinedGenerator uses yield* to yield values from both numberGenerator and an array.

Output of the code execution

Using yield* with Iterables

yield* can also be used to iterate over any iterable object (like arrays or strings) directly within a generator.

function* stringGenerator() {
    yield* "Hello";
}

const stringGen = stringGenerator();

for (let char of stringGen) {
    console.log(char); 
}

Here, yield* is used to iterate over and yield each character of a string.

Output of the code snippet for yield* with Iterables

Nested Generators

yield* can be especially powerful when used with nested generators, allowing for 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* seamlessly combines the outputs of multiple generators and an iterable.

Output for the code snippet of using yield* with nested generators

Conclusion

Thus we have quickly looked at how yield* enhances the functionality and flexibility of generators, allowing for more modular code and easy iteration over various types of iterables.

If you’d like to understand how we could use generators and promises in combination, this detailed article will be of use:

In Plain English 🚀

Thank you for being a part of the In Plain English community! Before you go:

JavaScript
Programming
Software Development
Software Engineering
Web Development
Recommended from ReadMedium