如何等待回调函数返回? [英] How to wait till callback function returns?
本文介绍了如何等待回调函数返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用phonegap安卓应用程序。我使用phonegap的存储api查询数据库。这里是我的代码:
I'm making an android application using phonegap. I'm using phonegap's Storage api for querying a database. here's my code:
function directPath(src, dest)
{
var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
db.transaction(queryDB, errorCB);
return arrayroute;
}
function queryDB(tx)
{
tx.executeSql(query, [], querySuccess, errorCB);
}
function querySuccess(tx,results) {
//Write some code here.
}
function errorCB(err) {
alert("Error in SQL: " + err);
}
问题是我想等待回调方法querySuccess完成执行,然后返回从directPath方法。
The problem is I want to wait till the callback method querySuccess finishes execution before returning from the directPath method.
推荐答案
不要试图打击异步:你的应用程序可能最终似乎对用户无反应。对任何必须执行的代码使用querySuccess回调。
Don't try to fight the asynchrony: Your app might end up seeming unresponsive to the user. Use the querySuccess callback for any code that has to be executed afterwards.
这篇关于如何等待回调函数返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文