How create global array

Hey!
I wanted to pass a dynamic value through an array to another group. Tell me is it possible?
Code example:

const blocing_user = [];

export function set () {
 
var inQ = uuidv4();

    blocing_user.push(inQ);

    console.log("Set# Size array " + blocing_user.length);
    console.log("Set# Add value " + inQ);


};

export function get () {
 
    var outQ = blocing_user.shift();

    console.log("Get# Size array " + blocing_user.length);
    console.log("Get# Get value " + outQ);
    
  };

But the Get function does not see the value:

INFO[0001] Set# Size array 1                             source=console
INFO[0001] Set# Add value c135c873-cd18-41fd-8090-de0c30f4eb7f  source=console
INFO[0002] Get# Size array 0                             source=console
INFO[0002] Get# Get value undefined                      source=console

Hi Paul,

Could you please take a look at this answer and tell us whether it solves the issue.

Thanks.

Inanc, thanks for your answer.
I want to fix main things:

  1. Every function creates it’s own virtual machine.
  2. Global variable available for reading and recording inside functions, but recording values are not available for other functions.
  3. Different ways of passing dynamic value:
    3.1 Using third party data store(for example http protocol/database)

Hi @PaulM,
I tried your code running the functions sequentially:

export default function() {
	set()
	get()
}

It works as expected for me returning the value on getting. How are you invoking the functions?

Can you also expand a bit on the various points, maybe adding some code examples, please?

Hi @codebien ,
Thanks for this example, it will help me a lot in the future.
Do I understand correctly that in your example the setup code will be one for two functions (set() and get()) ?
Example:

export const options = { //setup code
  stages: [
    { duration: '2m', target: 100 }, 
    { duration: '5m', target: 100 },
  ],
};

export default function() {
	set()
	get()
}

@PaulM,
can you clarify what do you mean by setup code, please? In k6 setup code is a very specific part where you define it by declaring a setup function. You can read about it here: Test lifecycle

Regarding your posted code seems you mean the options configuration and yes, it will be the same executed iteration function for all the stages.

If you would define different execution functions then you should use the exec property from the scenarios’ common option.