Guide

Async & Promise Patterns

RoExpress network calls are callback-based by default. Wrapping them in Promises lets you chain, parallel-merge, and cancel them cleanly.

Wrap a request

local Promise = require(ReplicatedStorage.Packages.Promise)

local function get(route, data)
    return Promise.new(function(resolve, reject)
        network:Get(route, data, function(res)
            if res.ok then resolve(res.data)
            else           reject(res.status) end
        end)
    end)
end

Chain

get("shop/catalogue")
    :andThen(function(data) UI:SetCatalogue(data); return get("player/data") end)
    :andThen(function(data) UI:SetCoins(data.coins) end)
    :catch(function(err)   UI:ShowError(err) end)

Parallel

Promise.all({
    get("shop/catalogue"),
    get("player/data"),
    get("leaderboard"),
}):andThen(function(results)
    local catalogue, playerData, board = results[1], results[2], results[3]
    UI:LoadAll(catalogue, playerData, board)
end)

Cancel on cleanup

local p = get("leaderboard"):andThen(...)

-- cancel if player leaves before response arrives
game.Players.LocalPlayer.AncestryChanged:Connect(function()
    p:cancel()
end)
Which Promise library? The examples use evaera's roblox-lua-promise (available on Wally as evaera/promise). Any library implementing the same API works.

See also

Network | callback API being wrapped  ·  Listener | similar pattern for push events  ·  Player Data | listener:Once as a simple single-promise pattern