Generated Token every 10 mins and use it during load test

Thank you @eyeveebe for your support
My scenario :

  • step 1: HTTP request //generate token here and use this token for next request , but this token is valid for 10mins only
  • step 2: http request and using token (generated in step1) in headers , this step I want do it for longer duration like 1 hour or 3hours , but after 10 mins test getting failed due invalid token. In step2 I want to use new token after every 10mins

How this is possible in K6 , can we try with while loop execute step2 10mins and exit , generate token and run step 2 or any possibility

I see below code from Periodically refreshing a token aside a script, how to use this in my scenario

var refreshed = 0;

export default function() {
	var diff = new Date() - refreshed 
	if (diff > 4999) {
		console.log(`refresh the token`)
		refreshed = new Date()