Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
Are JavaScript generators copyable / cloneable?

I am writing tests for a generator function that walks a tree. The next function accepts a boolean to determine if it should continue to explore that branch of the tree. I would like to be able to test it as follows:

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 (i.e. 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

This is important because otherwise you have to call the generator again with all the same arguments, e.g.:

// 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

An alternatively to some cloning function would be the ability to rewind a generator. A second alternative is to write a wrapper that records all of the arguments and (assuming the generator is pure) can replay the last n-1 arguments to get a generator in the previous state.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda