Skip to content

Instantly share code, notes, and snippets.

@amysimmons
Created June 12, 2018 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amysimmons/670ed152f290474d21cd07e643dcd8ec to your computer and use it in GitHub Desktop.
Save amysimmons/670ed152f290474d21cd07e643dcd8ec to your computer and use it in GitHub Desktop.
// define the generator function 
function *foo(x) {
	var y = x * (yield);
	return y;
}

// create the iterable to control the generator and pass in 6
var it = foo(6);

// start the generator, letting it run until the yield, so to var y = 6 * ...
it.next();

// unpause the generator, passing 7 to the waiting yield, and letting it run until the end
// capture the return value in res 
var res = it.next(7);

res // {value: 42, done: true}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment