local f1 = function(context, continue)
context.foo = 'foo'
print 'i am function one'
continue()
end
local f2 = function(context, continue)
context.bar = 'foo'
print 'i am function two'
continue()
end
local f3 = function(context, continue)
context.breno = 'magro'
print 'i am function three'
continue()
end
local f4 = function(context, continue)
context.cafil = 'ratos'
print 'i am function four'
print 'ia am the last function of the array'
end
local context = {}
local current_function_index = 0
local function_array = { f1, f2, f3, f4 }
local continue = function()
-- fetchs the current function of the 'function_array' variable
current_function_index = current_function_index + 1
print('index:' .. current_function_index)
local current_function = function_array[current_function_index]
-- tests to see if theres a next function to execute
if type(current_function) == 'nil' then
return error('can not call "continue" on last function of the chain')
end
-- executes the next function passing the context and the continuation
current_function(context, continue)
end
continue()
-----------
The idea is to trigger the execution of the first function of the 'function_array' variable
and then asynchronously trigger the next function in 'function_array' by
calling the 'continue' argument
I can see why the error is happening but I cant figure a way to get it to work.