Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
para... de un iterador sin cerrarlo

¿Es posible recorrer parte de un iterador con for...of sin cerrar el iterador cuando rompo el ciclo?

Ejemplo:

 function* numbers(i=0){ while(true) yield i++; } let nums=numbers(); // this loop prints numbers from 0 to 3 for(const n of nums){ if(n>3) break; console.log(n); } // this loop doesn't print anything because `nums` has already been closed for(const n of nums){ if(n>10) break; console.log(n); }

Sé que puedo pasar por el iterador por mi cuenta llamando a iterator.next() . Pero me pregunto si es posible hacer esto con la sintaxis for...of .

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

No. Sin embargo, puede proporcionar un contenedor que no reenvíe return() al generador:

 function unclosed(iterable) { return { [Symbol.iterator]() { const iterator = iterable[Symbol.iterator](); return { next: iterator.next.bind(iterator), return: null, throw: null, }; }, }; } function* numbers(i=0) { try { while (true) yield i++; } finally { console.log('done'); } } const nums = numbers(); // this loop prints numbers from 0 to 3 for (const n of unclosed(nums)) { console.log(n); if (n == 3) break; } // and this one the numbers from 4 to 10, as well as 'done' for (const n of nums) { console.log(n); if (n == 10) break; }

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!