如何解决Javascript中的异步问题?
如何修改以下代码以同步方式运行? AddonManager 是一个异步调用,我无法获得正确的结果。
function initWithPrivs (java, ext_id, jarFiles) {
var [loader, urls] = init(java, ext_id, jarFiles);
policyAdd(loader, urls);
return [loader, urls];
}
function init (java, ext_id, jarFiles) {
var classLoader;
var fURLs=[];
JAVA = java;
try {
Components.utils.import("resource://gre/modules/AddonManager.jsm");
AddonManager.getAddonByID("[email protected]",function(addon) {
//Some code to process fURLs and classLoader
//If i put return here its shows me error
}
);
return [classLoader,fURLs]; //If i use return here it return null.
}
catch(Exception e)
{
}
How can i modify the following code to run in a synchronous way ? The AddonManager is an asynchronous call and I could not able to get the proper result.
function initWithPrivs (java, ext_id, jarFiles) {
var [loader, urls] = init(java, ext_id, jarFiles);
policyAdd(loader, urls);
return [loader, urls];
}
function init (java, ext_id, jarFiles) {
var classLoader;
var fURLs=[];
JAVA = java;
try {
Components.utils.import("resource://gre/modules/AddonManager.jsm");
AddonManager.getAddonByID("[email protected]",function(addon) {
//Some code to process fURLs and classLoader
//If i put return here its shows me error
}
);
return [classLoader,fURLs]; //If i use return here it return null.
}
catch(Exception e)
{
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该使此代码同步,而应使用异步方法。
所以,我不建议这样做,但无论如何我都会提到它,你可以这样做:
You shouldn't make this code sync, use the async method.
So, I do not recommend doing this, but I'll mention it anyhow, you could do something like:
,这样做不是更好吗?
如果没有安装插件
Would it not be better to do:
in case the Addon is not installed?