Http asynchronous request in K6 load testing

Since I have asynchronous API call in my front-end, I have used http.asyncRequest method for API calls.

const response = await http.asyncRequest(“GET”, url, null, params);

However, I want the response of the above call, in order to call another API. When I run the load test second API call runs before completing this async request. Due to that second API call fail. Is there any way to handle this?

Other thing is this async requests are in a k6 group but when the result is showing it is not belong to the group that I was assigned. It showing in the outside of the group.

Hi @pamuducambio !

Welcome to the community forums! :wave:

Sure, there is a way!

The key thing to remember here is that http.asyncRequest returns a Promise, so there are several options for handling errors.

One option is to use .then and .catch, but since you use await, you could probably use just try...catch.

The other thing to consider is that even if your frontend application uses an async call, you need a response’s result to process the test run I believe it’s not a requirement to use asyncRequest. At the end, if you use await, you turn this code into a sync version.

Let me know if that answers.

Cheers!