'LinKin Posted March 18, 2015 Share Posted March 18, 2015 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. 1 Link to comment
Derpy Posted March 18, 2015 Share Posted March 18, 2015 umm store the function which gets result to a variable and then use return variable ? Link to comment
JR10 Posted March 19, 2015 Share Posted March 19, 2015 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
Gallardo9944 Posted March 19, 2015 Share Posted March 19, 2015 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
JR10 Posted March 19, 2015 Share Posted March 19, 2015 In order to make such functions you would need the data to be available as soon as you call the function, which is not the case here. Link to comment
'LinKin Posted March 19, 2015 Author Share Posted March 19, 2015 Yes, well the only solution would be to call another function or trigger an event inside the call back function Link to comment
DiSaMe Posted March 19, 2015 Share Posted March 19, 2015 Calling dbPoll with -1 timeout parameter will wait for response. That's how it will return the result to the calling function. If you instead want to suspend the execution of function and resume it when result is available (without forcing the whole server to wait), use coroutines (http://www.lua.org/manual/5.1/manual.html#2.11). 1 Link to comment
JR10 Posted March 20, 2015 Share Posted March 20, 2015 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 1 Link to comment
'LinKin Posted March 22, 2015 Author Share Posted March 22, 2015 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
JR10 Posted March 22, 2015 Share Posted March 22, 2015 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. 1 Link to comment
'LinKin Posted March 25, 2015 Author Share Posted March 25, 2015 Yes, ok JR10, really thanks a looot. You just saved me a lot of time! Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now