loadfileasync Asynchronous

Read a workspace Luau file on a background thread, then compile it without running it. Keep the returned function to run the code later.

Syntax
Luau
loadfileasync(path: string) -> (Function) OR (nil, error: string)

Parameters

Function parameters
ParameterTypeDescription
pathstringWorkspace-relative path of the Luau source file.

Returns

(Function) OR (nil, error: string)

A compiled function, or nil plus a compilation error string.

Usage notes

The file work runs on a background thread, and the call always yields at least one frame, even for a small file. Filesystem failures raise in the calling thread.

Compilation happens after the read, in the calling thread, with the same rules as loadstring. A syntax error returns nil and a message rather than raising.

Example

Example
Luau
writefile("async-chunk.luau", [[return "Compiled after a background read"]])
local chunk, reason = loadfileasync("async-chunk.luau")
if chunk then print(chunk()) else warn(reason) end
delfile("async-chunk.luau")