This is when I typically use the update tic. Update tic callbacks are "asynchronous". Your wait loop is "synchronous". It technically causes more churn, and locks the process until that loop ends.
Code:
local firstRunCalled = false
local firstRunComplete = false
local callFirst = function(...)
-- do whatever has to happen first
firstRunComplete = true
end
local callSecond = function()
-- do your second thing here
end
local mainFunction = function(eventType,...)
if firstRunCalled and eventType ~= nil then return end
-- eventType having a value means it came from the event handler, not our update tic.
-- Since we're already running, skip it until last pass completes
if not firstRunCalled and eventType == nil then return end
-- standard update tic but we're not processing
-- so kick us out
if not firstRunCalled then
firstRunCalled = true
firstRunComplete = false
callFirst( ... )
end
if not firstRunComplete then return end
-- kick us out if it isn't complete yet
firstRunCalled = false
callSecond()
end
EVENT_MANAGER:RegisterForUpdate("myaddon_mainfunction_updatetic", 100, mainFunction)
EVENT_MANAGER:RegisterForEvent( "myaddon_mainfunction_eventwatch", EVENT_WHATEVER, mainFunction)
Something like that, perhaps. Sorry, this is off the top of my head. Basically hook your mainFunction into both the event, and the update callback.