Skip to main content

Promise

Lightweight Promise class for asynchronous operations.

local promise: typeof(Promise.Promise)? -- Blank variable to allow rejection during a waterfall

promise = Promise.new(function(resolve, reject)
	task.wait(1)
	resolve(0)
end):andThen(function(num)
	return num + 1
end):andThen(function(num)
	promise:reject("error")
end):catch(function(err)
	warn(err)
end):finally(function(num)
	print("finally", promise.status)

	if promise.status ~= "rejected" then
		print(num)
	end
end) :: Promise.Promise

Install with wally by adding the following to your wally.toml:

Promise = "dig1t/promise@1.1.3"

Types

PromiseExecutor

interface PromiseExecutor {
resolveCallback--

Resolves the promise

rejectCallback--

Rejects the promise

}

Properties

statusType

Promise.statusType: table

Current status of the promise

Functions

new

Promise.new(
executorfunction--

The function to call when the promise is created.

) → Promise

Constructs a new Promise instance.

Promise.new(function(resolve, reject)
	task.wait(1)
	resolve(0)
end)

all

Promise.all(promises{Promise?}) → Promise

Creates a new promise that resolves when all of the promises in the given table have resolved.

resolve

Promise.resolve(resultany) → Promise

Creates a new promise that is resolved.

reject

Promise.reject(resultany) → Promise

Creates a new promise that is rejected.

withResolvers

Promise.withResolvers() → ()

Types

type Callback = (...any) → ()

Returns a new promise and the resolvers for the promise.

andThen

Promise:andThen(
executorfunction--

The function to call when the promise is fulfilled.

) → Promise

Resolves a promise with tuple parameters.

Called when the promise is fulfilled, if there are multiple fulfillment callbacks then the result of the last callback will waterfall into the next callback, if the promise rejects during the callback waterfall, then it will stop waterfalling.

The first callback will begin with the arguments from the resolution callback.

The promise can be rejected until the last fulfillment callback is called.

Promise:resolve(...)

catch

Promise:catch(
callbackfunction--

The function to call when the Promise is rejected.

) → Promise

Called when the Promise is rejected.

finally

Promise:finally(
callbackfunction--

The function to call when the promise is fulfilled.

) → Promise

Similar to andThen, finally will always be called at the end of the Promise and can only be set once, if the Promise is rejected then the callback run with return no parameters

Promise:resolve(...)

await

Promise:await() → (
boolean,--

Whether the promise was fulfilled or not.

any--

The result of the promise.

)

Awaits the promise and returns the result.

local success: boolean, result: any = Promise:await()

destroy

Promise:destroy() → ()

Destroys the Promise instance.

Promise:destroy()

Destroy

Cleanup
Promise:Destroy() → ()

Alias for promise:destroy()

Show raw api
{
    "functions": [
        {
            "name": "new",
            "desc": "Constructs a new Promise instance.\n\n```lua\nPromise.new(function(resolve, reject)\n\ttask.wait(1)\n\tresolve(0)\nend)\n```",
            "params": [
                {
                    "name": "executor",
                    "desc": "The function to call when the promise is created.",
                    "lua_type": "function"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 88,
                "path": "src/Promise/init.luau"
            }
        },
        {
            "name": "all",
            "desc": "Creates a new promise that resolves when all of the promises in the given table have resolved.",
            "params": [
                {
                    "name": "promises",
                    "desc": "",
                    "lua_type": "{ Promise? }"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 106,
                "path": "src/Promise/init.luau"
            }
        },
        {
            "name": "resolve",
            "desc": "Creates a new promise that is resolved.",
            "params": [
                {
                    "name": "result",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 155,
                "path": "src/Promise/init.luau"
            }
        },
        {
            "name": "reject",
            "desc": "Creates a new promise that is rejected.",
            "params": [
                {
                    "name": "result",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 162,
                "path": "src/Promise/init.luau"
            }
        },
        {
            "name": "withResolvers",
            "desc": "Returns a new promise and the resolvers for the promise.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise"
                },
                {
                    "desc": "",
                    "lua_type": "Callback"
                },
                {
                    "desc": "",
                    "lua_type": "Callback"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 169,
                "path": "src/Promise/init.luau"
            }
        },
        {
            "name": "_run",
            "desc": "Runs the Promise executor.\n\n```lua\nPromise:_run()\n```",
            "params": [],
            "returns": [],
            "function_type": "method",
            "ignore": true,
            "source": {
                "line": 192,
                "path": "src/Promise/init.luau"
            }
        },
        {
            "name": "_resolve",
            "desc": "Resolves a promise with tuple parameters.\n\n```lua\nPromise:resolve(...)\n```",
            "params": [
                {
                    "name": "...",
                    "desc": "The tuple parameters to resolve the promise with.",
                    "lua_type": "any"
                }
            ],
            "returns": [],
            "function_type": "method",
            "ignore": true,
            "source": {
                "line": 241,
                "path": "src/Promise/init.luau"
            }
        },
        {
            "name": "_reject",
            "desc": "Rejects a promise with tuple parameters.\n\n```lua\nPromise:reject(errorMessage)\n```",
            "params": [
                {
                    "name": "...",
                    "desc": "The tuple parameters to reject the promise with.",
                    "lua_type": "any"
                }
            ],
            "returns": [],
            "function_type": "method",
            "ignore": true,
            "source": {
                "line": 299,
                "path": "src/Promise/init.luau"
            }
        },
        {
            "name": "andThen",
            "desc": "Resolves a promise with tuple parameters.\n\nCalled when the promise is fulfilled, if there are multiple fulfillment callbacks\nthen the result of the last callback will waterfall into the next callback, if the promise rejects during the\ncallback waterfall, then it will stop waterfalling.\n\nThe first callback will begin with the arguments from the resolution callback.\n\nThe promise can be rejected until the last fulfillment callback is called.\n\n```lua\nPromise:resolve(...)\n```",
            "params": [
                {
                    "name": "executor",
                    "desc": "The function to call when the promise is fulfilled.",
                    "lua_type": "function"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 348,
                "path": "src/Promise/init.luau"
            }
        },
        {
            "name": "catch",
            "desc": "Called when the Promise is rejected.",
            "params": [
                {
                    "name": "callback",
                    "desc": "The function to call when the Promise is rejected.",
                    "lua_type": "function"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 375,
                "path": "src/Promise/init.luau"
            }
        },
        {
            "name": "finally",
            "desc": "Similar to andThen, finally will always be called at the end of the Promise\nand can only be set once, if the Promise is rejected then the callback run with return no parameters\n\n```lua\nPromise:resolve(...)\n```",
            "params": [
                {
                    "name": "callback",
                    "desc": "The function to call when the promise is fulfilled.",
                    "lua_type": "function"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 407,
                "path": "src/Promise/init.luau"
            }
        },
        {
            "name": "await",
            "desc": "Awaits the promise and returns the result.\n\n```lua\nlocal success: boolean, result: any = Promise:await()\n```",
            "params": [],
            "returns": [
                {
                    "desc": "Whether the promise was fulfilled or not.",
                    "lua_type": "boolean"
                },
                {
                    "desc": "The result of the promise.",
                    "lua_type": "any"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 444,
                "path": "src/Promise/init.luau"
            }
        },
        {
            "name": "destroy",
            "desc": "Destroys the Promise instance.\n\n```lua\nPromise:destroy()\n```",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 476,
                "path": "src/Promise/init.luau"
            }
        },
        {
            "name": "Destroy",
            "desc": "Alias for promise:destroy()",
            "params": [],
            "returns": [],
            "function_type": "method",
            "tags": [
                "Cleanup"
            ],
            "source": {
                "line": 489,
                "path": "src/Promise/init.luau"
            }
        }
    ],
    "properties": [
        {
            "name": "statusType",
            "desc": "Current status of the promise",
            "lua_type": "table",
            "source": {
                "line": 69,
                "path": "src/Promise/init.luau"
            }
        }
    ],
    "types": [
        {
            "name": "Callback",
            "desc": "",
            "lua_type": "(...any) -> ()",
            "source": {
                "line": 39,
                "path": "src/Promise/init.luau"
            }
        },
        {
            "name": "PromiseExecutor",
            "desc": "",
            "fields": [
                {
                    "name": "resolve",
                    "lua_type": "Callback",
                    "desc": "Resolves the promise"
                },
                {
                    "name": "reject",
                    "lua_type": "Callback",
                    "desc": "Rejects the promise"
                }
            ],
            "source": {
                "line": 61,
                "path": "src/Promise/init.luau"
            }
        }
    ],
    "name": "Promise",
    "desc": "Lightweight Promise class for asynchronous operations.\n\n```lua\nlocal promise: typeof(Promise.Promise)? -- Blank variable to allow rejection during a waterfall\n\npromise = Promise.new(function(resolve, reject)\n\ttask.wait(1)\n\tresolve(0)\nend):andThen(function(num)\n\treturn num + 1\nend):andThen(function(num)\n\tpromise:reject(\"error\")\nend):catch(function(err)\n\twarn(err)\nend):finally(function(num)\n\tprint(\"finally\", promise.status)\n\n\tif promise.status ~= \"rejected\" then\n\t\tprint(num)\n\tend\nend) :: Promise.Promise\n```\n\nInstall with wally by adding the following to your `wally.toml`:\n```toml\nPromise = \"dig1t/promise@1.1.3\"\n```",
    "source": {
        "line": 32,
        "path": "src/Promise/init.luau"
    }
}