# What does "yield" do?

**URL:** https://talk.observablehq.com/t/what-does-yield-do/4639
**Category:** Help
**Created:** [February 14, 2021, 2:20pm UTC](https://talk.observablehq.com/t/what-does-yield-do/4639 "2021-02-14T14:20:23Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![benjamesdavis](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/benjamesdavis/32/4048_2.png) [@benjamesdavis](https://talk.observablehq.com/u/benjamesdavis)
#### Post date: [February 14, 2021, 2:20pm UTC](https://talk.observablehq.com/t/what-does-yield-do/4639/1 "2021-02-14T14:20:23Z")

</div>

There seem to be 2 ways of rendering a chart in Observable:

1. Returning `Container.node()` at the end of your chart-drawing function

2. Calling your chart-drawing function within a block of html, e.g. [Elijah Meeks - Particle Sankey / Ben Davis / Observable](https://observablehq.com/@benjamesdavis/elijah-meeks-particle-sankey)

However, the latter will only work if you use “yield” before your html tags.  
E.g.:  
{yield html  
`<div id = 'container'> </div>`  
drawChart()}

Why is this? What job is `yield` doing here?

Many thanks,  
Ben

---

<div class="post-metadata">

### Author: ![mcmcclur](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mcmcclur/32/2364_2.png) [@mcmcclur](https://talk.observablehq.com/u/mcmcclur)
#### Post date: [February 14, 2021, 3:20pm UTC](https://talk.observablehq.com/t/what-does-yield-do/4639/2 "2021-02-14T15:20:50Z")

</div>

[`yield`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield) is a Javascript constructed used to pause and resume a generator `function*`. It’s described in the context of Observable in their [Introduction to Generators](https://observablehq.com/@observablehq/introduction-to-generators) notebook. It’s used in the [Particle Sankey notebook](https://observablehq.com/@benjamesdavis/elijah-meeks-particle-sankey) to allow the generator cell to run repeatedly to yield the animation.

---

<div class="post-metadata">

### Author: ![benjamesdavis](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/benjamesdavis/32/4048_2.png) [@benjamesdavis](https://talk.observablehq.com/u/benjamesdavis)
#### Post date: [February 14, 2021, 3:47pm UTC](https://talk.observablehq.com/t/what-does-yield-do/4639/3 "2021-02-14T15:47:20Z")

</div>

Thanks Mark! I’ll check out that notebook.

RE: the Particle Sankey notebook. Would the animation not work if the cell was run only run once, since the animations are driven by d3.timer?

---

<div class="post-metadata">

### Author: ![mcmcclur](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mcmcclur/32/2364_2.png) [@mcmcclur](https://talk.observablehq.com/u/mcmcclur)
#### Post date: [February 14, 2021, 4:22pm UTC](https://talk.observablehq.com/t/what-does-yield-do/4639/4 "2021-02-14T16:22:09Z")

</div>

> [@benjamesdavis](#):
>
> Would the animation not work if the cell was run only run once, since the animations are driven by d3.timer?

In fact, the flowing dots continue to run without a return, since they are driven by the timer. Everything in the `drawChart` command, before the definition of `tick`, though, seems to stop.

I don’t think that code is really idiomatic Observable - probably because it’s been ported from an old-style block.

---

<div class="post-metadata">

### Author: ![mbostock](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mbostock/32/9_2.png) [@mbostock](https://talk.observablehq.com/u/mbostock)
#### Post date: [February 15, 2021, 4:22pm UTC](https://talk.observablehq.com/t/what-does-yield-do/4639/5 "2021-02-15T16:22:30Z")

</div>

Here is an article on animation that may help:

> **[Animation Loops](https://observablehq.com/@mbostock/animation-loops)**
>
> There are several ways to implement animations in Observable. Each approach has its plusses and minuses, but a generator loop should be your default choice: it is concise, reasonably fast and the easiest to implement correctly. Option 1: Timer Loop...
