grafana/k6 · error
SharedArray constructor does not support async functions as…
Error message
SharedArray constructor does not support async functions as second argument
What it means
The second argument to new SharedArray is a builder function that k6 calls synchronously once, in the init context, to produce the shared data. Because the result must be available immediately for cross-VU serialization, the constructor explicitly rejects async functions via common.IsAsyncFunction before doing anything else.
Solutions
- Remove the async keyword - the builder must return the array synchronously: () => [...].
- If the builder awaits I/O, restructure it: load bytes synchronously with open() at init time (open() is synchronous in k6) and parse them inside a sync builder.
- For remote data, fetch it before the test (e.g. in a separate pre-step) and embed it in a local JSON file consumed by a sync builder.
Example fix
// before
new SharedArray('data', async () => JSON.parse(open('./data.json'))); // throws
// after
new SharedArray('data', () => JSON.parse(open('./data.json'))); Defensive patterns
Strategy: type-guard
Validate before calling
const isAsyncFn = (fn) => fn?.constructor?.name === 'AsyncFunction';
if (isAsyncFn(builder)) {
throw new Error('SharedArray builder must be synchronous; remove the async keyword');
} Type guard
const isSyncFunction = (fn) => typeof fn === 'function' && fn.constructor.name === 'Function';
Prevention
- Never write async in a SharedArray builder - k6 cannot await it in init.
- Do file I/O with the synchronous open() instead of fetch inside builders.
When it happens
Trigger: 'new SharedArray("name", async () => [...])' - an async function declared with the async keyword or an AsyncFunction constructor result. The error is thrown before the function is ever called.
Common situations: Refactoring data loaders that were originally async (fetch from a file, DB) into SharedArray builders; copy-pasting async open()/http code into the builder; IDE auto-adding async to the arrow function.
Related errors
- a function is expected as the second argument of…
- empty name provided to SharedArray's constructor
- group() does not support async functions as arguments…
- new SharedArray must be called in the init context
- only arrays can be made into SharedArray
AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18).
Data as JSON: /api/errors/6a7c6a3ce7ad90eb.
Report an issue: GitHub.
Appendix: source
Thrown at internal/js/modules/k6/data/data.go:88
const asyncFunctionNotSupportedMsg = "SharedArray constructor does not support async functions as second argument"
// sharedArray is a constructor returning a shareable read-only array
// indentified by the name and having their contents be whatever the call returns
func (d *Data) sharedArray(call sobek.ConstructorCall) *sobek.Object {
rt := d.vu.Runtime()
if d.vu.State() != nil {
common.Throw(rt, errors.New("new SharedArray must be called in the init context"))
}
name := call.Argument(0).String()
if name == "" {
common.Throw(rt, errors.New("empty name provided to SharedArray's constructor"))
}
val := call.Argument(1)
if common.IsAsyncFunction(rt, val) {
common.Throw(rt, errors.New(asyncFunctionNotSupportedMsg))
}
fn, ok := sobek.AssertFunction(val)
if !ok {
common.Throw(rt, errors.New("a function is expected as the second argument of SharedArray's constructor"))
}
builder := func() (sharedArray, error) { return getSharedArrayFromCall(rt, fn) }
array, err := d.shared.loadOrStore(name, builder)
if err != nil {
common.Throw(rt, err)
}
return array.wrap(rt).ToObject(rt)
}
// RecordReader is the interface that wraps the action of reading records from a resource.
//View on GitHub (pinned to 01ffac6f24)