Async / Await

A basic implementation of async/await functionality using coroutines to make the asynchronous requests look “synchronous”.

A dataserver request

an example of use, add the async/await code that is at the end of page

-- start async/await section

-- ===== COPY THE ASYNC/AWAIT CODE HERE =====

-- end async/await section


local function getName(username)
    local id = await(ll.RequestUserKey(username))
    local displayName = if uuid(id).istruthy then await(ll.RequestDisplayName(id)) else "not found"
    ll.OwnerSay(displayName)
end

async(getName, "suzannalinn")

dataserver and http requests

an example of use, add the async/await code that is at the end of page

-- start async/await section

-- ===== COPY THE ASYNC/AWAIT CODE HERE =====

-- end async/await section


local function getInfo(userId)
    local bornDate = await(ll.RequestAgentData(userId, DATA_BORN))
    local rating = await(ll.RequestSimulatorData(ll.GetRegionName(), DATA_SIM_RATING))
    ll.OwnerSay(`you are born in {bornDate} and this is a {rating} region`)
end

local function getQuote()
    local quote = await(ll.HTTPRequest("https://zenquotes.io/api/random", {}, ""))
    local json = lljson.decode(quote)
    ll.OwnerSay(`\n{json[1].q}\n{json[1].a}\n `)
end

LLEvents:on("touch_start", function(events)
    async(getInfo, events[1]:getKey())
    async(getQuote)
end)

async/await code

-- Async / Await (by Suzanna Linn, 2025-09-11)

-- start async/await section

local _awaiting = {}

local function async(func, ...)
    coroutine.resume(coroutine.create(func), ...)
end

local function awaiting(queryid, ...)
    if _awaiting[queryid] then
        coroutine.resume(_awaiting[queryid], ...)
        _awaiting[queryid] = nil
    end
end

local function await(queryid)
    _awaiting[queryid] = coroutine.running()
    return coroutine.yield()
end

LLEvents:on("dataserver", awaiting)
LLEvents:on("http_response", function(request_id, status, metadata, body)
    awaiting(request_id, body)
end)

-- end async/await section


-- ===== PLACE YOUR CODE HERE =====


async/await with error control code

-- Async / Await with error control (by Suzanna Linn, 2025-11-03)

-- start async/await section

local _awaiting = {}

local function _err(err, message)
    if not err then
        local func, line = debug.info(3,"nl")
        message = `\nerror in coroutine called in {if func ~= "" then "function ".. func else "main script"} at line {line}:\n{message}\n`
        error(message, 3)
    end
end

local function async(func, ...)
    _err(coroutine.resume(coroutine.create(func),...))
end

local function awaiting(queryid, ...)
    if _awaiting[queryid] then
        _err(coroutine.resume(_awaiting[queryid], ...))
        _awaiting[queryid] = nil
    end
end

local function await(queryid)
    _awaiting[queryid] = coroutine.running()
    return coroutine.yield()
end

LLEvents:on("dataserver", awaiting)
LLEvents:on("http_response", function(request_id, status, metadata, body)
    awaiting(request_id, body)
end)

-- end async/await section


-- ===== PLACE YOUR CODE HERE =====