{"record":{"id":"6a7c6a3ce7ad90eb","repo":"grafana/k6","slug":"sharedarray-constructor-does-not-support-async-fun","errorCode":null,"errorMessage":"SharedArray constructor does not support async functions as second argument","messagePattern":"SharedArray constructor does not support async functions as second argument","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/data/data.go","lineNumber":88,"sourceCode":"const asyncFunctionNotSupportedMsg = \"SharedArray constructor does not support async functions as second argument\"\n\n// sharedArray is a constructor returning a shareable read-only array\n// indentified by the name and having their contents be whatever the call returns\nfunc (d *Data) sharedArray(call sobek.ConstructorCall) *sobek.Object {\n\trt := d.vu.Runtime()\n\n\tif d.vu.State() != nil {\n\t\tcommon.Throw(rt, errors.New(\"new SharedArray must be called in the init context\"))\n\t}\n\n\tname := call.Argument(0).String()\n\tif name == \"\" {\n\t\tcommon.Throw(rt, errors.New(\"empty name provided to SharedArray's constructor\"))\n\t}\n\tval := call.Argument(1)\n\n\tif common.IsAsyncFunction(rt, val) {\n\t\tcommon.Throw(rt, errors.New(asyncFunctionNotSupportedMsg))\n\t}\n\n\tfn, ok := sobek.AssertFunction(val)\n\tif !ok {\n\t\tcommon.Throw(rt, errors.New(\"a function is expected as the second argument of SharedArray's constructor\"))\n\t}\n\n\tbuilder := func() (sharedArray, error) { return getSharedArrayFromCall(rt, fn) }\n\tarray, err := d.shared.loadOrStore(name, builder)\n\tif err != nil {\n\t\tcommon.Throw(rt, err)\n\t}\n\n\treturn array.wrap(rt).ToObject(rt)\n}\n\n// RecordReader is the interface that wraps the action of reading records from a resource.\n//","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/data/data.go#L70-L106","documentation":"SharedArray serializes the builder function's return value once, synchronously, during the init context. An async builder would return a Promise that cannot be serialized and shared, so data.go:87-90 rejects async functions with asyncFunctionNotSupportedMsg.","triggerScenarios":"new SharedArray('x', async () => [...]); a builder that awaits something (file reads, fetches, timers) — anything declared with async or returning a Promise.","commonSituations":"Copying async open()/readFile patterns from k6/experimental modules into SharedArray builders; attempting HTTP calls or async I/O during init; refactors that add await inside the builder.","solutions":["Make the builder synchronous: precompute data at init time (e.g. JSON.parse(open('data.json'))) and return it from a plain function","Remove every await/async from the builder; do any async work elsewhere and pass results in","If async data loading is unavoidable, load it before the constructor call site at top level and wrap the final plain array"],"exampleFix":"// before\nnew SharedArray('users', async () => {\n  return JSON.parse(open('./users.json')); // async -> rejected\n});\n\n// after\nconst users = JSON.parse(open('./users.json'));\nnew SharedArray('users', function () { return users; });","handlingStrategy":"validation","validationCode":"// Static discipline: ensure the builder is not declared async and contains no await.\n// Load data synchronously at init, then wrap:\nconst rows = JSON.parse(open('./rows.json'));\nnew SharedArray('rows', function () { return rows; });","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never use async/await inside the SharedArray builder","Do async I/O elsewhere and pass the resulting plain array into the builder","Use open() + JSON.parse for file-based init data — it is synchronous by design"],"tags":["shared-array","async","init-context","k6"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}