Terminate the current script and optionally return a value to the caller.
exit([value])
value (optional, any type) - Value to return to the caller. Meaning depends on context.Never returns (terminates script execution)
The value passed to exit() has different meanings depending on how the script was invoked:
The value becomes the HTTP response:
ctx = context()
req = ctx.request()
exit({
"status" = 200,
"body" = "response body",
"headers" = {"Content-Type" = "text/plain"}
})
If no exit() is called, the response is 204 No Content.
The value becomes the return value from run():
// worker.du
exit({status = "done", value = 42})
// main script
result = run("worker.du")
print(result.value) // 42
The script terminates (value is ignored):
// spawned.du
exit({status = "completed"}) // Value is not captured
Exits Duso (value is ignored):
print("Doing work...")
exit() // Duso exits with status 0
Returning data from a worker:
// worker.du
data = [1, 2, 3, 4, 5]
sum = 0
for item in data do
sum = sum + item
end
exit(sum) // Returns 15
// main
result = run("worker.du")
print("Sum: " + result) // 15
HTTP handler returning JSON:
ctx = context()
if ctx then
users = [
{id = 1, name = "Alice"},
{id = 2, name = "Bob"}
]
exit({
"status" = 200,
"body" = format_json(users),
"headers" = {"Content-Type" = "application/json"}
})
end
Self-referential server cleanup:
ctx = context()
if ctx == nil then
server = http_server({port = 8080})
server.route("GET", "/")
server.start()
// Cleanup after server stops
print("Server shutdown complete")
exit(0)
end
// Handler code
req = ctx.request()
exit({status = 200, body = "Hello"})
exit() terminates the current script immediatelyexit() will executespawn()), the value is lostrun() script, the value becomes the return value for the callerexit() without a value returns nil to the caller