Defer
Creates a temporary scope that cleans up automatically after the callback executes.
- TypeScript
- Luau
const result = vault.Defer((scope) => {
const part = new Instance("Part")
scope.Add(part) // Will be cleaned after callback
const connection = scope.Connect(part.Touched, () => {
print("Touched")
})
return "some result"
})
local result = vault:Defer(function(scope)
local part = Instance.new("Part")
scope:Add(part) -- Will be cleaned after callback
local connection = scope:Connect(part.Touched, function()
print("Touched")
end)
return "some result"
end)
Parameters
| Parameter | Type | Description |
|---|---|---|
callback | VaultDeferCallback | Function that receives a temporary scope vault |
Returns
Cleanable \| undefined - The callback's return value, or undefined if error
Behavior
- Creates a child vault and passes it to the callback
- All resources added to the scope are automatically cleaned after callback
- If callback errors, logs the error and returns undefined
- Perfect for temporary operations that need cleanup