Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is yield in JavaScript

Understanding the Concept of "Yield"

When you're learning programming, there are certain concepts that can seem a little bit like magic. "Yield" in JavaScript is one of those. But don't worry, we're going to break it down so you can understand it easily.

Think of "yield" as the director of a movie. The director controls when the actors start their scenes and when they stop, allowing other scenes to take place. Similarly, "yield" can pause and resume a function in JavaScript at any time. This is quite unique because traditionally, once a function starts in programming, it runs to the end without interruption.

In technical terms, "yield" is a keyword used in generator functions. But don't let that intimidate you. Generator functions are just a type of function that can be stopped and started again, while maintaining its context. Quite like pausing a movie and resuming it from where you left off.

Let's look at a simple example:

function* generatorFunction() { 
    yield 'Hello, ';
    yield 'World!';
}

const generator = generatorFunction();

console.log(generator.next().value); // 'Hello, '
console.log(generator.next().value); // 'World!'
console.log(generator.next().value); // undefined

In this code, the function generatorFunction is a generator function. We know it's a generator because of the asterisk (*) after the "function" keyword. Inside this function, we have two "yield" statements.

When we call generator.next().value, it runs the function until it hits a "yield", then stops. The value of the "yield" (in this case, 'Hello, ') is returned. The next time we call generator.next().value, it resumes the function from where it stopped, and runs until it hits the next "yield". This cycle can continue until there are no more "yield" statements left.

How "Yield" is Useful

Yield can be incredibly useful in scenarios where you want to control the flow of your code in a fine-grained manner. Because yield allows you to pause a function, it gives you the ability to create complex, asynchronous code that is much easier to read and understand.

Consider this example:

function* fetchUserAvatar(userId) {
  const user = yield fetch(`https://api.example.com/user/${userId}`);
  const profile = yield user.json();
  return profile.avatar;
}

const generator = fetchUserAvatar('123');

generator.next().value
  .then(user => generator.next(user).value)
  .then(profile => generator.next(profile).value)
  .then(avatar => console.log(avatar));

In this case, we're using "yield" to pause our generator function while it waits for data from the server. This allows us to write asynchronous code that looks synchronous, making it easier to understand.

Closing Thoughts

The concept of "yield" in JavaScript, like many programming concepts, can be a bit daunting at first. However, once you get the hang of it, the possibilities it opens up for your code are vast.

Remember, "yield" is like the director of a movie, controlling when each scene (or in this case, line of code) begins and ends. With "yield", you're the director of your code. You're in control. You decide when to start, when to pause, and when to resume.

So, take your newfound knowledge and start directing! Who knows, you might just create the next blockbuster...in code!