Does code in groups (after HTTP calls) run synchronously?

I understand from the documentation that calls such as k6.http run synchronously.

However, does code in groups also run synchronously, both intra-group and inter-group?

An example where I am curious would be:

  group("visit type A", function () {
    http.get(BASE_URL + "example.html");
    const response = http.batch([
      ["GET", BASE_URL + "topbar.html"],
      ["GET", BASE_URL + "navbar.html"],
    ]);

    console.log(1);
  }

  console.log(2);

  group("visit type B", function () {
    http.get(BASE_URL + "example2.html");
    const response = http.batch([
      ["GET", BASE_URL + "topbar.html"],
      ["GET", BASE_URL + "navbar.html"],
    ]);

    console.log(3);
  }

  console.log(4);

I would assume this would work synchronously, based on an answer here.

My intention is to replace these console.log() statements with code which manages internal state of a modelled user (e.g., the total number of items bought in a demo shopping application), conditionally based on responses.

Hello! Yes, the code here runs synchronously, i.e. you should see 1->2->3->4 in your log output. The http.batch() call runs synchronously as well, i.e. it only returns once all of the requests in the batch have received a response (or have timed out).

2 Likes

Thanks, Tom! Building upon this, is there a nicer way to get variables from a group scope into its parent scope other than:

  let var1;
  group("visit type A", function () {
    http.get(BASE_URL + "example.html");
    const response = http.batch([
      ["GET", BASE_URL + "topbar.html"],
      ["GET", BASE_URL + "navbar.html"],
    ]);

    var1 = response[0].status;
  }

Is it okay to wrap the group in a promise (i.e., resolve(response[0].status))? Then I could chuck the wrapped group functions in an api.js/api.ts file and use async/await syntax. :slight_smile:

Unfortunately, this is not possible at the moment, k6 doesn’t have event loops (and thus, async or promises) in yet. Instead, every VU is a completely separate and independent JS runtime. We also plan to introduce event loops and proper async support though, eventually… :sweat_smile: Follow https://github.com/loadimpact/k6/issues/882 for updates.