Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

202
Vistas
¿Los generadores de JavaScript son copiables/clonables?

Estoy escribiendo pruebas para una función generadora que recorre un árbol. La next función acepta un valor booleano para determinar si debe continuar explorando esa rama del árbol. Me gustaría poder probarlo de la siguiente manera:

 let tree_walker = make_tree_walking_generator(some_arguments) let result = tree_walker.next() test(result.value, "0", "Test it returns root element") // I assume this is not possible yet / ever? let tree_walker_clone = clone_generator_and_state(tree_walker) // <-- result = tree_walker.next(true) // true === first element was consumed (ie it was all that was needed) test(result.value, undefined, "Test it returns undefined if last element was consumed") test(result.done, true) // should be finished // Restore the original state captured in the clone tree_walker = tree_walker_clone result = tree_walker.next(false) // false === first element was not consumed test(result.value, "0->1", "Test it returns the next element if the last element was not consumed") test(result.done, false) // should not be finished

Esto es importante porque, de lo contrario, debe volver a llamar al generador con los mismos argumentos, por ejemplo:

 // get the same starting generator again tree_walker = make_tree_walking_generator(some_arguments) // Bring it back up to the same penultimate position (n-1) result = tree_walker.next() // Continue testing from this point. The problem is the previous // call is made twice and for a deep tree this might be many 10s of // calls with complex arguments. result = tree_walker.next(false) // false === first element was not consumed test(result.value, "0->1", "Test it returns the next element if the last element was not consumed") test(result.done, false) // should not be finished

Una alternativa a alguna función de clonación sería la capacidad de rebobinar un generador. Una segunda alternativa es escribir un contenedor que registre todos los argumentos y ( suponiendo que el generador sea puro ) pueda reproducir los últimos n-1 argumentos para obtener un generador en el estado anterior.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda