Jump to content

Return a value from a callback function


'LinKin

Recommended Posts

Hello,

I've a function (sqlCheck) that executes a dbQuery using a callback function (myCallBackFunc).

In the callback function (myCallBackFunc), I process the result etc...

But I want to return a value from this callback function (myCallBackFunc) to the function that first called it (sqlCheck).

Is it possible? Most likely it's not, just making sure.

  • Like 1
Link to comment

The thing is, callbacks are not called immediately, which means that the rest of the code will have been executed by the time the callback is called.

Why would you want this? You can just do whatever you want to do with the data inside the callback.

Link to comment

Making such a long chain is not really necessary. You can make multiple basic functions (e.g. i have setProfileData, getProfileData, giveProfileData (for numbers), takeProfileData (for numbers) and others) and operate with them in an export. Callbacks and other things become useless then.

Link to comment

This might explain what CrystalMV meant:

local db_coroutine = coroutine.create(function() 
 dbQuery(callback, database, "SELECT * FROM table"); 
 local queryHandle = coroutine.yield(); 
 local result = dbPoll(queryHandle, 0); 
 -- more code 
end); 
coroutine.resume(db_coroutine); 
  
function callback(queryHandle) 
 coroutine.resume(db_coroutine, queryHandle); 
end 

This is what happens: you first create your coroutine by passing it its main function, we then start the coroutine by running coroutine.resume, our function gets executed, the first line (dbQuery) gets called and then the coroutine yields (waits until another call to coroutine.resume is ran). In our callback, we run coroutine.resume once more passing it our queryHandle, which will be sent to coroutine.yield and the rest of the function will get called.

It's more than okay if you don't understand any of that, coroutine and multithreading are not exactly simple. Remember that all you really need is to just use callbacks.

More links for coroutines tutorials:

http://www.lua.org/pil/9.html

http://lua-users.org/wiki/CoroutinesTutorial

  • Like 1
Link to comment

I think I get it. So the when the coroutine gets resumed, the code above the "coroutine.yield();" is not executed, but the code below it is executed. Am I right?

Also, what if I have 2 coroutine.yields inside that function and the coroutine yields by the second time, when the function is executed again, is it capable to understand that it must continue with the code-execution that is below the second coroutine.yield?

Link to comment

You're right.

Yes, if you have 2 coroutine.yield, it will stop twice and will continue from that very respective yield.

The idea is actually very simple, a call to coroutine.yield will pause and wait for a return, which only happens through coroutine.resume, much like using executeSQLQuery or dbPoll with -1, it pauses everything and wait for the result and then continues with the rest of the code.

  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...