function Promise.new(h)local e={_source=debug.traceback(),_type="\080\114\111\109\105\115\101",_status=Promise.Status.Started,_value=nil,_unhandledRejection=false,_queuedResolve={};_queuedReject={}}setmetatable(e,Promise)local function A(...)e:_resolve(...)end local function q(...)e:_reject(...)end local E,o=wpcall(h,A,q)if not E and e._status==Promise.Status.Started then q(o)end return e end

function Promise.new(callback)
	local promise = {
		-- Used to locate where a promise was created
		_source = debug.traceback(),

		-- A tag to identify us as a promise
		_type = "Promise",

		_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
		_value = nil,

		-- If an error occurs with no observers, this will be set.
		_unhandledRejection = false,

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

	setmetatable(promise, Promise)

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

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

	local ok, err = wpcall(callback, resolve, reject)

	if not ok and promise._status == Promise.Status.Started then
		reject(err)
	end

	return promise
end
