function Promise._new(W,q,s)if s~=nil and not Promise.is(s)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 h={_source=W;_status=Promise.Status.Started,_values=nil;_valuesLength=-1;_unhandledRejection=true,_queuedResolve={};_queuedReject={};_queuedFinally={};_cancellationHook=nil;_parent=s,_consumers=setmetatable({},MODE_KEY_METATABLE)}if s and s._status==Promise.Status.Started then s._consumers[h]=true end setmetatable(h,Promise)local function x(...)h:_resolve(...)end local function K(...)h:_reject(...)end local function X(W)if W then if h._status==Promise.Status.Cancelled then W()else h._cancellationHook=W end end return h._status==Promise.Status.Cancelled end;(coroutine.wrap(function()local W,s,R=runExecutor(h._source,q,x,K,X)if not W then K(R[1])end end))()return h end function Promise.new(W)return Promise._new(debug.traceback(nil,2),W)end function Promise.__tostring(W)return string.format("\080\114\111\109\105\115\101\040\037\115\041",W._status)end
--[[
	Constructs a new Promise with the given initializing callback.

	This is generally only called when directly wrapping a non-promise API into
	a promise-based version.

	The callback will receive 'resolve' and 'reject' methods, used to start
	invoking the promise chain.

	Second parameter, parent, is used internally for tracking the "parent" in a
	promise chain. External code shouldn't need to worry about this.
]]
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
