task
Built-in task scheduler & thread spawning
Example usage
Section titled “Example usage”local task = require("@lune/task")
-- Waiting for a certain amount of timetask.wait(1)print("Waited for one second")
-- Running a task after a given amount of timetask.delay(2, function() print("Ran after two seconds")end)
-- Spawning a new task that runs concurrentlytask.spawn(function() print("Running instantly") task.wait(1) print("One second passed inside the task")end)
print("Running after task.spawn yields")Functions
Section titled “Functions”cancel
Section titled “cancel”function task.cancel(thread: thread) endStops a currently scheduled thread from resuming.
Parameters
thread- The thread to cancel
function task.defer<T...>(functionOrThread: thread | (T...) -> ...any, ...: T...): threadDefers a thread or function to run at the end of the current task queue.
Parameters
functionOrThread- The function or thread to defer
Returns
- The thread that will be deferred
function task.delay<T...>( duration: number, functionOrThread: thread | (T...) -> ...any, ...: T...): threadDelays a thread or function to run after duration seconds.
Parameters
functionOrThread- The function or thread to delay
Returns
- The thread that will be delayed
function task.spawn<T...>(functionOrThread: thread | (T...) -> ...any, ...: T...): threadInstantly runs a thread or function.
If the spawned task yields, the thread that spawned the task will resume, letting the spawned task run in the background.
Parameters
functionOrThread- The function or thread to spawn
Returns
- The thread that was spawned
function task.wait(duration: number?): numberWaits for at least the given amount of time.
The minimum wait time possible when using task.wait is limited by the underlying OS sleep implementation.
For most systems this means task.wait is accurate down to about 5 milliseconds or less.
Parameters
duration- The amount of time to wait
Returns
- The exact amount of time waited
