Typesafe Promise implementation for Roblox Luau
Find a file
Reselim 49deee5d51
Fix default.project.json to allow usage as submodule (#29)
* Fix default.project.json to allow usage as submodule

* Rename lua-promise to just promise

* Change promise to Promise
2021-03-19 18:50:11 -04:00
.github Create FUNDING.yml 2020-04-24 17:51:14 -04:00
.vuepress Add installation guide 2020-08-17 19:30:25 -04:00
lib Fix linting (#49) 2021-03-18 20:43:49 -04:00
modules Update testez 2020-07-10 23:45:10 -04:00
.editorconfig Initial dump 2018-01-25 17:21:58 -08:00
.gitignore Update .gitignore 2020-08-24 13:20:03 -04:00
.gitmodules Remove unused files 2020-05-04 22:07:36 -04:00
.luacheckrc Start implementing tests, add lemur and testez dependencies 2018-01-31 15:29:04 -08:00
CHANGELOG.md v3.1.0 2020-12-01 20:26:44 -05:00
default.project.json Fix default.project.json to allow usage as submodule (#29) 2021-03-19 18:50:11 -04:00
LICENSE Update LICENSE 2019-09-10 00:29:21 -04:00
package-lock.json Bump elliptic from 6.5.3 to 6.5.4 (#53) 2021-03-18 20:45:36 -04:00
package.json Update vuepress 2020-12-01 20:26:32 -05:00
README.md Update README.md 2020-08-17 19:31:08 -04:00
rotriever.toml v3.1.0 2020-12-01 20:26:44 -05:00
runTests.server.lua Update testez 2020-05-04 22:07:55 -04:00
test.project.json Fix default.project.json to allow usage as submodule (#29) 2021-03-19 18:50:11 -04:00

Roblox Lua Promise

An implementation of Promise similar to Promise/A+.

View docs

Why you should use Promises

The way Roblox models asynchronous operations by default is by yielding (stopping) the thread and then resuming it when the future value is available. This model is not ideal because:

  • Functions you call can yield without warning, or only yield sometimes, leading to unpredictable and surprising results. Accidentally yielding the thread is the source of a large class of bugs and race conditions that Roblox developers run into.
  • It is difficult to deal with running multiple asynchronous operations concurrently and then retrieve all of their values at the end without extraneous machinery.
  • When an asynchronous operation fails or an error is encountered, Lua functions usually either raise an error or return a success value followed by the actual value. Both of these methods lead to repeating the same tired patterns many times over for checking if the operation was successful.
  • Yielding lacks easy access to introspection and the ability to cancel an operation if the value is no longer needed.

This Promise implementation attempts to satisfy these traits:

  • An object that represents a unit of asynchronous work
  • Composability
  • Predictable timing

Example

Promise.new returns synchronously.

local HttpService = game:GetService("HttpService")

-- A light wrapper around HttpService
-- Ideally, you do this once per project per async method that you use.
local function httpGet(url)
	return Promise.new(function(resolve, reject)
		local result = HttpService:JSONDecode(HttpService:GetAsync(url))

		if result.ok then
			resolve(result.data)
		else
			reject(result.error)
		end
	end)
end

-- Usage
httpGet("https://some-api.example")
	:andThen(function(body)
		print("Here's the web api result:", body)
	end)
	:catch(function(err)
		warn("Web api encountered an error:", err)
	end)