--[=[
	Rejects the promise with the values given
	@param ... T -- Params to reject with
]=]

function Promise:Reject(...)
	self:_reject({...}, select("#", ...))
end

function Promise:_reject(values, valuesLength)
	if not self._pendingExecuteList then
		return
	end

	self._rejected = values
	self._valuesLength = valuesLength

	local list = self._pendingExecuteList
	self._pendingExecuteList = nil
	for _, data in pairs(list) do
		self:_executeThen(unpack(data))
	end

	-- Check for uncaught exceptions
	if self._unconsumedException and self._valuesLength > 0 then
		task.defer(function()
			-- Yield to end of frame, giving control back to Roblox.
			-- This is the equivalent of giving something back to a task manager.
			if self._unconsumedException then
				local errOutput = self:_toHumanReadable(self._rejected[1])

				if ENABLE_TRACEBACK then
					warn(("[Promise] - Uncaught exception in promise\n\n%q\n\n%s")
						:format(errOutput, self._source))
				else
					warn(("[Promise] - Uncaught exception in promise: %q")
						:format(errOutput))
				end
			end
		end)
	end
end

function Promise:_toHumanReadable(data)
	if type(data) == "table" then
		local errOutput
		local ok = pcall(function()
			errOutput = HttpService:JSONEncode(data)
		end)
		if not ok then
			errOutput = tostring(data)
		end
		return errOutput
	else
		return tostring(data)
	end
end
