Futures/Async Runtime - 57e11a - 01-06-2024
Hey everyone! I've been working on a library for futures/promises/async/whateverthehellyouwanttocallthem. It's built on coroutines and has a bunch of logic for jumping into and out of them.
fut API:- fut.async(fn: func(args...) -> ?) -> (func(args...) -> Future<?>)
Create an async function, when called, returns a future that wakes when the function returns with the given return value.
- fut.new() -> Future<T>, func(value: T, silent: bool)
Returns a future and a function that wakes the future with a given value. Pass true into the silent parameter to prevent an error when trying to wake the future after it's already been woken.
- fut.join(futures: Future<?>[]) -> Future<?[]>
"Joins" the futures into one, when all of them have woken, the returned future will wake with an array of values corresponding to the returned values of the given futures (in the same order).
- fut.race(futures: Future<?>[]) -> Future<?>
"Races" the futures into one, the first that wakes will wake the returned future with its' value.
- fut.delay(time: number, value: ?) -> Future<?>
Helper function to create a future that wakes with the given value after the given amount of time.
Future API:- Future<T>:await() -> T
Must run inside an async function
Suspends the current async function until the future wakes (akin to await promise in JavaScript)
- Future<T>:on(func(value: T))
Runs a function when the future wakes (akin to promise.then(value => ...) in JavaScript)
Example:
--@dependency STEAM_0:1:180222680 anisu/lib/fut.txt async
local fut = require('anisu/lib/fut.txt')
local inner = fut.async(function(value)
print('inner before')
fut.delay(5):await()
print('inner after')
return value
end)
local main = fut.async(function(you, can, have, args)
--[[--
output:
1 2 3 4
--]]--
print(you, can, have, args)
--[[--
output:
outer before
inner before
<5s wait>
inner after
42
outer after
--]]--
print('RUNNING TEST sequence')
print('outer before')
print(inner(42):await())
print('outer after')
--[[--
output:
outer before
inner before
outer after invoke
<5s wait>
inner after
42
outer after await
--]]--
print('RUNNING TEST simul')
print('outer before')
local f = inner(42)
print('outer after invoke')
print(f:await())
print('outer after await')
--[[--
output:
before
<2s wait>
42
after
--]]--
print('RUNNING TEST race')
print('before')
-- wakes after first future wakes
print(fut.race {
fut.delay(1, 42),
fut.delay(2, 69),
}:await())
print('after')
--[[--
output:
before
<1s wait>
{42, 69}
after
--]]--
print('RUNNING TEST join')
print('before')
-- wakes after last future wakes
print(fut.join {
fut.delay(1, 42),
fut.delay(2, 69),
}:await())
print('after')
--[[--
output:
before
<2s wait>
after
--]]--
print('RUNNING TEST custom delay')
-- literally the implementation of fut.delay
local function delay(time, value)
local f, wake = fut.new()
timer.simple(time, function() wake(value) end)
return f
end
print('before')
delay(2):await()
print('after')
return 69
end)
-- prints 69 after the rest of everything is done
main(1, 2, 3, 4):on(print)
|