This is a problem I sometimes come across in developing and I was wondering if anyone has an elegant and versatile solution that I can adopt as best practice.
I will sometimes be calling several functions that preform different actions of unknown durations and I want to call an onComplete only for the final one that is executed. The problem lies in the fact that any one of the functions could take any amount of time but I definitely want to call the onComplete when the last one finishes. And because of the structuring of my functions I want to keep the generic callback system and be able to pass the onComplete into each function. I guess I could set a boolean to denote whether the function was called or not but that would get messy.
The problem applies to many scenarios so this example is a simplified generic version.
local function finalOnComplete( event ) print( 'I only want to call finalOnComplete once') end local function one( onComplete ) timer.performWithDelay( math.random(1000), onComplete ) end local function two( onComplete ) timer.performWithDelay( math.random(1000), onComplete ) end local function three( onComplete ) timer.performWithDelay( math.random(1000), onComplete ) end one( finalOnComplete ) two( finalOnComplete ) three( finalOnComplete )
So is there a nice way to ensure that the onComplete only gets called once? Some kind of event based solution or attaching tags to functions?? My mind has gone blank with this.