
// Coroutine function that returns a promise that resolves when the coroutine
// is finished. Accepts a generator function, and assumes that everything
// yielded from the generator function is a promise.
module.exports = function coroutine(generatorFunction) {
  let gen = generatorFunction()
  return new Promise(resolve => {
    // Handle every single value yielded by the generator function.
    function step(value) {
      let result = gen.next(value)
      if (result.done) {
        // Generator is done, so handle its return value.
        resolve(result.value)
      } else {
        // Generator is not done, so result.value is a promise.
        let promise = result.value
        // When the promise is done, run this function again.
        promise.then(newValue => step(newValue))
      }
    }
    step(undefined)
  })
}

@@ -288,16 +288,17 @@
 ction()%0A
+%0A
   return
@@ -350,21 +350,22 @@
  single 
-value
+result
  yielded
@@ -409,56 +409,22 @@
 ion 
-step(value) %7B%0A      let result = gen.next(value)
+next(result) %7B
 %0A   
@@ -482,14 +482,18 @@
  so 
-handle
+resolve to
  its
@@ -750,56 +750,126 @@
  =%3E 
-step(newValue))%0A      %7D%0A    %7D%0A    step(undefined
+%7B%0A          let newResult = gen.next(newValue)%0A          next(newResult)%0A        %7D)%0A      %7D%0A    %7D%0A%0A    next(gen.next()
 )%0A  

Refactor so that next() takes the return value of gen.next()