Use of async func in a generator function

by Arnaud Buchholz

JavaScript

async function asyncFunc (value) {
  await new Promise((resolve) => setTimeout(resolve, 250));
  console.log('asyncFunc called with', value);
}

async function * generatorFunc () {
  const result = yield 'first value';
  await asyncFunc(result);
  yield 'last value';
  console.log('last step of the generator function');
  return 'terminated';
}

const iterator = generatorFunc();
let i = 0;
do {
  const { value, done } = iterator.next(++i);
  console.log('Iteration', i, ' value', value, ' done', done);
  if (done) {
    break;
  }
} while (true)