function Promise._new(J,p,A)if A~=nil and not Promise.is(A)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 G={_source=J,_status=Promise.Status.Started,_values=nil;_valuesLength=-1;_unhandledRejection=true,_queuedResolve={},_queuedReject={};_queuedFinally={},_cancellationHook=nil;_parent=A;_consumers=setmetatable({},MODE_KEY_METATABLE)}if A and A._status==Promise.Status.Started then A._consumers[G]=true end setmetatable(G,Promise)local function W(...)G:_resolve(...)end local function Q(...)G:_reject(...)end local function H(J)if J then if G._status==Promise.Status.Cancelled then J()else G._cancellationHook=J end end return G._status==Promise.Status.Cancelled end;(coroutine.wrap(function()local J,A,E=runExecutor(G._source,p,W,Q,H)if not J then Q(E[1])end end))()return G end function Promise.new(J)return Promise._new(debug.traceback(nil,2),J)end function Promise.__tostring(J)return string.format("\080\114\111\109\105\115\101\040\037\115\041",J._status)end

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 = {
		-- 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

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

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

	return self
end

function Promise.new(executor)
	return Promise._new(debug.traceback(nil, 2), executor)
end

function Promise:__tostring()
	return string.format("Promise(%s)", self._status)
end
