JSFiddle - React, Tailwind, and code Playground

by msfrisbie

JavaScript

class Emitter {
  constructor(max) {
    this.max = max;
    this.syncIdx = 0;
    this.asyncIdx = 0;
  }

  *[Symbol.iterator]() {
    while(this.syncIdx < this.max) {
      yield this.syncIdx++;
    }
  }
  
  async *[Symbol.asyncIterator]() {
    while(this.asyncIdx < this.max) {
      yield this.asyncIdx++;
    }
  }
}

const emitter = new Emitter(5);

async function asyncIteratorSyncCount() {
  const syncCounter = emitter[Symbol.iterator]();
  
  console.log(syncCounter[Symbol.asyncIterator]);
  
  for await(const x of syncCounter) {
    console.log(x);
  }
}

asyncIteratorSyncCount();
// 0
// 1
// 2
// 3
// 4


/* 
function syncIteratorAsyncCount() {
  const asyncCounter = emitter[Symbol.asyncIterator]();
  
  debugger;
  
  for (const x of asyncCounter) {
    console.log(x);
  }
}

syncIteratorAsyncCount(); */
// 0
// 1
// 2
// 3
// 4