What's the yield keyword in JavaScript?

what'd
what meaning
what definition
what w
what does
what is called
how to pronounce what
what h

I heard about a "yield" keyword in JavaScript, but I found very poor documentation about it. Can someone explain me (or recommend a site that explains) its usage and what it is used for?

The MDN documentation is pretty good, IMO.

The function containing the yield keyword is a generator. When you call it, its formal parameters are bound to actual arguments, but its body isn't actually evaluated. Instead, a generator-iterator is returned. Each call to the generator-iterator's next() method performs another pass through the iterative algorithm. Each step's value is the value specified by the yield keyword. Think of yield as the generator-iterator version of return, indicating the boundary between each iteration of the algorithm. Each time you call next(), the generator code resumes from the statement following the yield.

What, P!nk's official music video for 'So What'. Click to listen to P!nk on Spotify: http:// smarturl.it Duration: 3:46 Posted: Oct 25, 2009 WhatsApp Messenger: More than 2 billion people in over 180 countries use WhatsApp to stay in touch with friends and family, anytime and anywhere. WhatsApp is free and offers simple, secure, reliable messaging and calling, available on phones all over the world.

Late answering, probably everybody knows about yield now, but some better documentation has come along.

Adapting an example from "Javascript's Future: Generators" by James Long for the official Harmony standard:

function * foo(x) {
    while (true) {
        x = x * 2;
        yield x;
    }
}

"When you call foo, you get back a Generator object which has a next method."

var g = foo(2);
g.next(); // -> 4
g.next(); // -> 8
g.next(); // -> 16

So yield is kind of like return: you get something back. return x returns the value of x, but yield x returns a function, which gives you a method to iterate toward the next value. Useful if you have a potentially memory intensive procedure that you might want to interrupt during the iteration.

P!nk - So What (Official Music Video), I will do what I can to help you. TranslationsEdit. show ▽what? (interrogative pronoun)� WhatsApp Messenger: More than 2 billion people in over 180 countries use WhatsApp to stay in touch with friends and family, anytime and anywhere. WhatsApp is free and offers simple, secure, reliable messaging and calling, available on phones all over the world.

what, Quickly send and receive WhatsApp messages right from your computer. Ask.com is the #1 question answering service that delivers the best answers from the web and real people - all in one place.

WhatsApp Web, what definition: 1. used to ask for information about people or things: 2. used in questions that show you are…. Learn more. Instead of funding a police department, a sizable chunk of a city's budget is invested in communities, especially marginalized ones where much of the policing occurs.

To give a complete answer: yield is working similar to return, but in a generator.

As for the commonly given example, this works as follows:

function *squareGen(x) {
    var i;
    for (i = 0; i < x; i++) {
        yield i*i;
    }
}

var gen = squareGen(3);

console.log(gen.next().value); // prints 0
console.log(gen.next().value); // prints 1
console.log(gen.next().value); // prints 4

But theres also a second purpose of the yield keyword. It can be used to send values to the generator.

To clarify, a small example:

function *sendStuff() {
    y = yield (0);
    yield y*y;
}

var gen = sendStuff();

console.log(gen.next().value); // prints 0
console.log(gen.next(2).value); // prints 4

This works, as the value 2 is assigned to y, by sending it to the generator, after it stopped at the first yield (which returned 0).

This enables us to to some really funky stuff. (look up coroutine)

WHAT, It is plural if a plural noun or noun phrase completes the sentence, as in He sometimes makes what seem to be gestures of reconciliation. � Clauses with what as� Seen a font in use and want to know what it is? Upload an image to WhatTheFont to find the closest matches in our database.

What, What's behind Trump's bizarre suggestion. Opinion by Richard Galant, CNN. Updated 9:27 AM ET, Sun August 2, 2020. CTW Andrew Yang Coronavirus Trump� This will clear your Bing search history on this device. You can't undo it.

What's behind Donald Trump's bizarre suggestion (opinion), U.S. law requires employers to create a workplace free from discrimination and harassment. But as offices go virtual, what happens when staff� More than just great coffee. Explore the menu, sign up for Starbucks® Rewards, manage your gift card and more.

What to Do When Your Employee Is Harassed Online, Eat What You Want. Eat What You Want is Gaby’s 3rd cookbook, due out Spring 2020 and it's all about recipes for real life: no deprivation, no starvation, just Gaby's signature mash-up of bright flavors when you want it, and hearty and satisfying when you need it.

Comments
  • He probably means 'Yield' bytes.com/topic/python/answers/685510-yield-keyword-usage
  • it's explained in MDN, but I think this only works for firefox, right? How portable is it? Any way to to this on Chrome or node.js? PD: sorry, it's Javascript v1.7+, so that's the property to look at when looking for support.
  • @Trylks: Generators are available in Node since v0.11.2
  • @JanusTroelsen however, only behind a flag. They are supported natively in ioJS
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • @NicolasBarbulesco there is a very obviously-placed example if you click through to the MDN documentation.
  • @MattBall - would a function as javascript for PI like this be sufficient as follows: function * PI { PI = (( Math.SQRT8; ) / 9801;); } - or is there already a function implemented in javascript for this calculation of PI ?
  • What's the point of quoting MDN here? I think everyone can read that on MDN. Visit davidwalsh.name/promises to learn more about them.
  • How did this get ~80 upvotes when (a) it is a copy of the "very poor documentation" as the questioner calls it and (b) it says nothing helpful? Far better answers below.
  • if someone asks for explanation, just copy pasting a documentation is totally unuseful. Asking means that you already searched in docs but you did not understand them.
  • Helpful, but i guess you its function* foo(x){ there