Не работает асинхронный итератор,скажите,почему?

const ite = {
  [Symbol.asyncIterator](){
  let wert = 0;
   return {
    async next(){
      await new Promise((resolve) => setTimeout(resolve, 1000));
      if(this.wert<=4){
     return {done: false, value: wert++};
     }else{
      return {done: true};
     }
    }
   };
  }
};

(async () => {
  for await (const perebor of ite){
   console.log(perebor);
  }

})();

Просто ничего не выводит


Ответы (1 шт):

Автор решения: Grundy

Проблема в this.wert. Так как wert - это просто переменная, то и обращаться надо к ней напрямую, как ты это делаешь в строке wert++:

const ite = {
  [Symbol.asyncIterator]() {
    let wert = 0;
    return {
      async next() {
        await new Promise((resolve) => setTimeout(resolve, 1000));
        if (wert <= 4) {
          return {
            done: false,
            value: wert++
          };
        } else {
          return {
            done: true
          };
        }
      }
    };
  }
};

(async() => {
  for await (const perebor of ite) {
    console.log(perebor);
  }

})();

→ Ссылка