Skip to main content

DeferAsync

Creates a temporary scope for async operations that cleans up automatically after all promises resolve.

const result = await vault.DeferAsync(async (scope) => {
const part = new Instance("Part")
scope.Add(part)

const data = await fetchData()
return data
})

Parameters

ParameterTypeDescription
callback(scope: VaultDefer) => Promise<T>Async function that receives a scope

Returns

Promise<T \| undefined> - The callback's resolved value, or undefined if error

Behavior

  • Creates a child vault for the async operation
  • Tracks all promises and cleans up only when all complete
  • Scope resources are automatically cleaned after promises resolve/reject
  • Handles both sync errors (pcall) and async errors (promise catch)
  • Perfect for async operations with temporary resources