--[=[
	@param instance Instance
	@param timeout number?
	@return Promise<componentInstance>
	Waits for a component instance to exist on the given Roblox instance.
]=]

function Component:WaitFor(instance, timeout)
	local isName = (type(instance) == "string")
	local function IsInstanceValid(obj)
		return ((isName and obj.Instance.Name == instance) or ((not isName) and obj.Instance == instance))
	end
	for _,obj in ipairs(self._objects) do
		if IsInstanceValid(obj) then
			return Promise.resolve(obj)
		end
	end
	local lastObj = nil
	return Promise.fromEvent(self.Added, function(obj)
		lastObj = obj
		return IsInstanceValid(obj)
	end):andThen(function()
		return lastObj
	end):timeout(timeout or DEFAULT_WAIT_FOR_TIMEOUT)
end

