Async core

Core Module - Async - An extention of task and token to allow a single require to register and run async functions.

Usage


-- To use Async you must register the allowed functions when the file is loaded, often this will just be giving access to
-- some functions within a module if you expect that at part may be blocked by in game permissions or a custom system you have made
-- you may also want to register functions that you want to have a time delay, such as waiting 2 seconds before printing a message

-- When player.admin is called (either command or gui element event) by a player who isnt admin then it will error
-- here we register the function to promote the player so that it will run async and outside the player scope
local promote_player =
Async.register(function(player)
    player.admin = true
end)

-- This will allow us to bypass the error by running one tick later outside of any player scope
Async(promote_player, game.player)

-- Here we make an sync function that we want to have a delay, note the delay is not defined here
local print_message =
Async.register(function(player, message)
    player.print(message)
end)

-- We can then call the async function with a delay using the wait function
Async.wait(60, print_message, game.player, 'One second has passed!')

Dependencies

utils.task
utils.token

Functions

register(callback) Register a new async function, must called when the file is loaded
run(token[, ...]) Runs an async function, you may supply any number of arguments as required by that function
wait(ticks, token[, ...]) Runs an async function after the given number of ticks, you may supply any number of arguments as required by that function

Dependencies

# utils.task
# utils.token

Functions

# register(callback)

Register a new async function, must called when the file is loaded

Parameters:
  • callback : (function) the function that can be called as an async function
Returns:
  • (string) the uid of the async function which can be passed to Async.run and Async.wait
Usage:
-- Registering a function to set the admin state of a player
local set_admin =
Async.register(function(player, state)
    if player.valid then
        player.admin = state
    end
end)
-- Registering a function to print to a player
local print_to_player =
Async.register(function(player, message)
    if player.valid then
        player.print(message)
    end
end)
# run(token[, ...])

Runs an async function, you may supply any number of arguments as required by that function

Parameters:
  • token : (string) the token of the async function you want to run
  • ... : (any) the other params that you want to pass to your function (optional)
Usage:
-- Make a player admin regardless of if you are admin
Async.run(set_admin, player, true)
# wait(ticks, token[, ...])

Runs an async function after the given number of ticks, you may supply any number of arguments as required by that function

Parameters:
  • ticks : (number) the number of ticks that you want the function to run after
  • token : (string) the token of the async function you want to run
  • ... : (any) the other params that you want to pass to your function (optional)
Usage:
-- Print a message to a player after 5 seconds
Async.wait(300, print_to_player, 'Hello, World!')