local O={Error=Error;Status=makeEnum("\080\114\111\109\105\115\101\046\083\116\097\116\117\115",{"\083\116\097\114\116\101\100","\082\101\115\111\108\118\101\100";"\082\101\106\101\099\116\101\100";"\067\097\110\099\101\108\108\101\100"}),_getTime=os.clock;_timeEvent=(game:GetService("\082\117\110\083\101\114\118\105\099\101")).Heartbeat,_unhandledRejectionCallbacks={}}O.prototype={}O.__index=O.prototype function O._new(U,m,Z)if Z~=nil and not O.is(Z)then error("\065\114\103\117\109\101\110\116 \035\050 \116\111 \080\114\111\109\105\115\101\046\110\101\119 \109\117\115\116 \098\101 \097 \112\114\111\109\105\115\101 \111\114 \110\105\108",2)end local v={_thread=nil,_source=U;_status=O.Status.Started;_values=nil;_valuesLength=-1,_unhandledRejection=true,_queuedResolve={},_queuedReject={};_queuedFinally={},_cancellationHook=nil;_parent=Z,_consumers=setmetatable({},MODE_KEY_METATABLE)}if Z and Z._status==O.Status.Started then Z._consumers[v]=true end setmetatable(v,O)local function K(...)v:_resolve(...)end local function M(...)v:_reject(...)end local function g(U)if U then if v._status==O.Status.Cancelled then U()else v._cancellationHook=U end end return v._status==O.Status.Cancelled end v._thread=coroutine.create(function()local O,U,Z=runExecutor(v._source,m,K,M,g)if not O then M(Z[1])end end)task.spawn(v._thread)return v end

local Promise = {
	Error = Error,
	Status = makeEnum("Promise.Status", { "Started", "Resolved", "Rejected", "Cancelled" }),
	_getTime = os.clock,
	_timeEvent = game:GetService("RunService").Heartbeat,
	_unhandledRejectionCallbacks = {},
}
Promise.prototype = {}
Promise.__index = Promise.prototype

function Promise._new(traceback, callback, parent)
	if parent ~= nil and not Promise.is(parent) then
		error("Argument #2 to Promise.new must be a promise or nil", 2)
	end

	local self = {
		-- The executor thread.
		_thread = nil,

		-- Used to locate where a promise was created
		_source = traceback,

		_status = Promise.Status.Started,

		-- A table containing a list of all results, whether success or failure.
		-- Only valid if _status is set to something besides Started
		_values = nil,

		-- Lua doesn't like sparse arrays very much, so we explicitly store the
		-- length of _values to handle middle nils.
		_valuesLength = -1,

		-- Tracks if this Promise has no error observers..
		_unhandledRejection = true,

		-- Queues representing functions we should invoke when we update!
		_queuedResolve = {},
		_queuedReject = {},
		_queuedFinally = {},

		-- The function to run when/if this promise is cancelled.
		_cancellationHook = nil,

		-- The "parent" of this promise in a promise chain. Required for
		-- cancellation propagation upstream.
		_parent = parent,

		-- Consumers are Promises that have chained onto this one.
		-- We track them for cancellation propagation downstream.
		_consumers = setmetatable({}, MODE_KEY_METATABLE),
	}

	if parent and parent._status == Promise.Status.Started then
		parent._consumers[self] = true
	end

	setmetatable(self, Promise)

	local function resolve(...)
		self:_resolve(...)
	end

	local function reject(...)
		self:_reject(...)
	end

	local function onCancel(cancellationHook)
		if cancellationHook then
			if self._status == Promise.Status.Cancelled then
				cancellationHook()
			else
				self._cancellationHook = cancellationHook
			end
		end

		return self._status == Promise.Status.Cancelled
	end

	self._thread = coroutine.create(function()
		local ok, _, result = runExecutor(self._source, callback, resolve, reject, onCancel)

		if not ok then
			reject(result[1])
		end
	end)

	task.spawn(self._thread)

	return self
end
