Hold up execution until event or something.

Perhaps this is a strange question.

Whenever there are callbacks, code gets written backwards and is very nested and that bugs me. For example;

local f2 = function() print "LAST" end local f1 = function() print "FIRST"; timer.performWithDelay(100,f2) end local runMyFuncs = function()     timer.performWithDelay(100,f1)     -- returns immediately end runMyFuncs() print "AFTER"

If I run this, it will print AFTER, then FIRST, then LAST. because runMyFuncs returns immediately.

Is there anyway that I can make execution hold up until something is complete? Basically so it works like this;

local f2 = function() print "LAST"; \*\*EXECUTION GOES FROM HERE\*\*; end local f1 = function() print "FIRST"; timer.performWithDelay(100,f2) end local runMyFuncs = function()     timer.performWithDelay(100,f1)     \*\*TO HERE\*\* end runMyFuncs() print "AFTER"

It isn’t just with timers, it’s network calls and anything where there is a callback.

The code always ends up in this strange nested backwards spaghetti - callbacks, in callbacks, in callbacks.

Probably it’s just how LUA is, I don’t know.

Set up a runtime listener and check for the condition before it does anything.

The backwards coding (needing to define something before you use it) is a Lua feature. It’s a single pass compiler and variable/object names won’t get memory addresses until they are seen during the compile.  Languages like C/C++ are two pass systems where the code is scanned, objects/variables assigned addresses, then the 2nd pass compiles the code with all the known addresses.

The async nature of Corona isn’t so much Lua as it is being built on an event driven system and the desire to not block the User Interface while work is being done. Users expect a responsive UI from mobile apps. You would not get good reviews if your app blocks until long running processes complete.

Dealing with event driven systems adds complexity no doubt. But the end result is a better user experience.

Rob

Set up a runtime listener and check for the condition before it does anything.

The backwards coding (needing to define something before you use it) is a Lua feature. It’s a single pass compiler and variable/object names won’t get memory addresses until they are seen during the compile.  Languages like C/C++ are two pass systems where the code is scanned, objects/variables assigned addresses, then the 2nd pass compiles the code with all the known addresses.

The async nature of Corona isn’t so much Lua as it is being built on an event driven system and the desire to not block the User Interface while work is being done. Users expect a responsive UI from mobile apps. You would not get good reviews if your app blocks until long running processes complete.

Dealing with event driven systems adds complexity no doubt. But the end result is a better user experience.

Rob