Reputation: 956
I'd like to write a function, which will be look like this:
public function execute(sql:String) {
sessionDBConn.open(writeSessionDB);
createStmt.sqlConnection = sessionDBConn;
createStmt.text = sql;
createStmt.addEventListener(SQLEvent.RESULT, onResult);
createStmt.addEventListener(SQLErrorEvent.ERROR, onError);
createStmt.execute();
}
And I want this function to wait until SQLLite statement is done. All I have now is that onResult is invoked, when executing of statement is completed, but code, when I execute execute(sql)
move on to the next line immediately.
What can I do? Thanks in advance.
Upvotes: 0
Views: 263
Reputation: 3728
Events are asynchronous, and as such, you cannot halt all other code execution while waiting for a response.
One solution would be to refactor your code so that all code that follows the call to execute
is called from within your onResult
method.
You could also implement some form of callback framework, such that your execute
method would accept a second paramater of type Function
:
public function execute(sql:String, callback:Function) {
sessionDBConn.open(writeSessionDB);
createStmt.sqlConnection = sessionDBConn;
createStmt.text = sql;
createStmt.addEventListener(SQLEvent.RESULT, callback);
createStmt.addEventListener(SQLErrorEvent.ERROR, onError);
createStmt.execute();
}
Then, you could place all relevant code in whatever method you pass to the execute
method.
Upvotes: 2