Typesafe Promise implementation for Roblox Luau
Find a file
eryn L. K 01499e5b7f
Merge pull request #19 from Quenty/users/quenty/avoid_realloc
Avoid reallocating another table for the _consumers() metatable, lead…
2020-03-29 18:16:49 -04:00
.vuepress Make cancellation propagate downstream 2019-09-12 03:58:56 -04:00
lib Avoid reallocating another table for the _consumers() metatable, leading to a 5%-10% speed up of the trivial Promise.new(function() end) case 2020-03-29 03:35:15 -07:00
modules Start implementing tests, add lemur and testez dependencies 2018-01-31 15:29:04 -08:00
.editorconfig Initial dump 2018-01-25 17:21:58 -08:00
.gitignore Update for release 2019-09-10 15:34:06 -04:00
.gitmodules Start implementing tests, add lemur and testez dependencies 2018-01-31 15:29:04 -08:00
.luacheckrc Start implementing tests, add lemur and testez dependencies 2018-01-31 15:29:04 -08:00
.luacov Add .gitignore and .luacov 2018-02-01 11:21:41 -08:00
CHANGELOG.md Fix rejection propagation issue 2019-11-12 23:07:43 -05:00
default.project.json Add traceback piping to more functions 2020-02-12 18:55:29 -05:00
LICENSE Update LICENSE 2019-09-10 00:29:21 -04:00
package-lock.json Make cancellation propagate downstream 2019-09-12 03:58:56 -04:00
package.json Make cancellation propagate downstream 2019-09-12 03:58:56 -04:00
README.md Update README.md 2019-09-12 04:00:39 -04:00
rotriever.toml Make all/race cancel correctly 2019-09-28 00:13:30 -04:00
spec.lua Start implementing tests, add lemur and testez dependencies 2018-01-31 15:29:04 -08: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.async 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.async(function(resolve, reject)
		local ok, result = pcall(HttpService.GetAsync, HttpService, url)

		if ok then
			resolve(result)
		else
			reject(result)
		end
	end)
end

-- Usage
httpGet("https://google.com")
	:andThen(function(body)
		print("Here's the Google homepage:", body)
	end)
	:catch(function(err)
		warn("We failed to get the Google homepage!", err)
	end)