Team Spartan Cookies & Milk Forums
  • Issue Tracker
  • Donations
  • Search
  • Member List
  • Help

Register

Team Spartan Cookies & Milk Forums Garry's Mod Starfall v
1 2 3 4 5 … 18 Next »
Release Futures/Async Runtime
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Threaded Mode
Release Futures/Async Runtime
01-06-2024, 09:58 PM, (This post was last modified: 01-06-2024, 10:00 PM by 57e11a. Edited 2 times in total.)
#1
57e11a Offline
Cheating Donating Member
***
Posts: 198
Threads: 68
Joined: Apr 2019
Futures/Async Runtime
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)

she/her
Founder of The Anisu Corporation
[Image: iZUefQi.gif]
Find
Reply
« Next Oldest | Next Newest »


  • View a Printable Version
Forum Jump:


Users browsing this thread: 1 Guest(s)
Contact UsTeam Spartan Cookies & MilkReturn to TopLite (Archive) ModeMark all forums readRSS Syndication

Powered By MyBB, © 2002-2026 MyBB Group.