Why this "wait until button click" not working?


Please see the btn2
{
  for(let value of Generators.input(btn2))
  {
    await value;
    console.log(Date.now())
  }
}

Why everytime run the cell calls the console.log?
Why run the cell multiple times, when the button clicked, the console.log will be called multiple times?

I indent btn2 not be a viewof.

I have fixed the code like this:

{
  let gen = Generators.input(btn2)
  await gen.next().value; //await is not neccessary. Just call next() is enough.
  try
  {
    for(let value of gen)
    {
      yield value;
      console.log(Date.now());
    }
  }
  finally
  {
    gen.return();
  }
}

I know that if I use I infinite async function, there’s no way to dispose the generator.
But I really don’t know why I must ignore the first generator value.

1 Like

okok, I know why.
Because btn2.value is not undefined. Then Generators.input will generator the value.
This will help viewof operator has the first initial value.

Now I understand this example https://observablehq.com/@mbostock/wait-until-button .
It is not pause the evaluation at the button statement. But reference a not evaluated button value, that makes that whole cell pause evaluation. Once button value is evaluated, this cell will run immediate whenever how many times the cell runs, it will never wait button click again.

1 Like

If all you need is a button, then Mike’s example is actually a tiny bit over-engineered. A shorter form, along with some explanations, can be found here: