Pages iterator function contains breaking yield

Issue Description

Iterator functions aren’t allowed to yield. The iterPageItems utility function on Pages documentation calls AdvanceToNextPageAsync which yields, making the function unable to iterate anything past the first page. The yield needs to be handled outside the iterator.

I wrote a function to handle pages. Albeit being longer than the original sample because of the necessity of the expanded code, it has the same end result and does not run into the same yielding issue. I’m adding it here in case IX likes this and wants to use it. So as not to confuse novice developers, I’ve added comments and no typechecking.

Iterator function:

-- Reformat pages as tables
local function pagesToTable(pages)
	local items = {}
	while true do
		table.insert(items, pages:GetCurrentPage())
		if pages.IsFinished then
			break
		end
		pages:AdvanceToNextPageAsync()
	end
	return items
end

local function iterPageItems(pages)
	local contents = pagesToTable(pages)
	-- Track the current page number starting at 1
	local pageNum = 1
	-- Get last page number so we don't iterate over it
	local lastPageNum = #contents
	
	-- for will resume this coroutine until there's nothing to go through
	return coroutine.wrap(function ()
		-- Loop until page number is greater than last page number
		while pageNum <= lastPageNum do
			-- Go through all the entries of the current page
			for _, item in ipairs(contents[pageNum]) do
				-- Pause loop to let developer handle entry and page number
				coroutine.yield(item, pageNum)
			end
			pageNum += 1
		end
	end)
end

Ran into this issue while taking the iterPageItems code sample while trying to work with a Pages object. Thinking back, this is also what sparked an issue in the past while I was trying to work with DataStore listing.

Issue Area: Documentation Content
Page URL: Pages | Roblox Creator Documentation

7 Likes

Thanks for the report! We’ll follow up when we have an update for you.

2 Likes

Apologies for the super late response. Just wanted to let you know we updated the page and resolved the reported issue. Thanks!

4 Likes