metis.async
A basic system for running multiple tasks at the same time.
One creates asynchronous tasks with async
, whose result can be fetched with
await
.
Usage
local async = require "metis.async" async.run(function() local left = async(function() print("Start 1") sleep(0.5) print("Done 1") return 123 end) local right = async(function() print("Start 2") sleep(1) error("Oh no") print("Done 2") return 456 end) print("Awaiting") local left, right = left:await(), right:await() print(left .. right) end)
async(f) | Run a function in the background. |
---|---|
await(task) | Wait for a task to finish. |
run() | Start the task queue. |
- async(f)Source
Run a function in the background.
Parameters
- f function() The function to run
Returns
Task
The generated task.
- await(task)Source
Wait for a task to finish.
Parameters
- task
Task
The task to await on.
Returns
- ... The result of running this task.
Throws
If the underlying task errored.
If the "terminate" event was found.
- task
- run()Source
Start the task queue. This can either be given a function (which should run the main body of your function), or called as the last statement in your program.
Tasks will not be run until this function is called, so you should not
await
(or ideally create any tasks) until the queue is created.Returns
- The result of the given function.
Throws
If the underlying function throws.
If trying to run multiple task queues at once.
Types
Task
An asynchronous task.