{"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":"validation","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/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/data/data.go#L70-L106","documentation":"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.","triggerScenarios":"'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.","commonSituations":"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.","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."],"exampleFix":"// before\nnew SharedArray('data', async () => JSON.parse(open('./data.json'))); // throws\n\n// after\nnew SharedArray('data', () => JSON.parse(open('./data.json')));","handlingStrategy":"type-guard","validationCode":"const isAsyncFn = (fn) => fn?.constructor?.name === 'AsyncFunction';\nif (isAsyncFn(builder)) {\n  throw new Error('SharedArray builder must be synchronous; remove the async keyword');\n}","typeGuard":"const isSyncFunction = (fn) => typeof fn === 'function' && fn.constructor.name === 'Function';","tryCatchPattern":null,"preventionTips":["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."],"tags":["k6","sharedarray","async","constructor"],"backgroundTag":"async-function-unsupported","analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}