k6 icon indicating copy to clipboard operation
k6 copied to clipboard

Using a generator as default function

Open mstoykov opened this issue 1 year ago • 5 comments

Now that k6 supports generator the following code

export default function*  () {
  console.log(1);
  yield 1;
  console.log(2);
  yield 2;
  console.log(3);
  yield 3;
}

is valid and will not do anything when run with 10 iterations as executing the generator function returns a Generator.

Arguably this should just give a ~warning~error.

Same for setup, teardown and handleSummary

mstoykov avatar Apr 06 '23 13:04 mstoykov

Arguably this should just give a warning.

Hmm I think it should be a validation error, not just a warning. Is there a reason to support generators for any function that is specified in a scenario.exec?

na-- avatar Apr 06 '23 13:04 na--

Ah yeah, I mis~spoke~typed - it should be an error

mstoykov avatar Apr 06 '23 13:04 mstoykov

Is there a reason to support generators for any function that is specified in a scenario.exec?

You can argue that the above should run until hte first yield on the first iteration, until the second on the second one and so on.

I don't particularly thing this is useful though, and likely will be a bit invovled to implement. Even more so to actually document and find a reasonable use case

mstoykov avatar Apr 06 '23 13:04 mstoykov

Good points, something like that might be useful :thinking: However, if we start with throwing an error now, we can always relax that constraint in the future, if we decide to support use cases like that.

na-- avatar Apr 06 '23 13:04 na--

After an internal discussion, we decided to clean a milestone since the issue was jumping between milestones without completion.

Once we determine which milestone it lands, we set the right one.

olegbespalov avatar Nov 16 '23 12:11 olegbespalov