如何解决Javascript中的异步问题?

发布于 2024-12-08 14:21:17 字数 792 浏览 0 评论 0原文

如何修改以下代码以同步方式运行? 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

永不分离 2024-12-15 14:21:17

您不应该使此代码同步,而应使用异步方法。

所以,我不建议这样做,但无论如何我都会提到它,你可以这样做:

var addonObj = null;
AddonManager.getAddonByID("[email protected]",function(addon) {
    addonObj = addon;
});
var thread = Cc["@mozilla.org/thread-manager;1"].getService().currentThread;
while (addonObj == null)
  thread.processNextEvent(true);
// addonObj is no longer null...

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:

var addonObj = null;
AddonManager.getAddonByID("[email protected]",function(addon) {
    addonObj = addon;
});
var thread = Cc["@mozilla.org/thread-manager;1"].getService().currentThread;
while (addonObj == null)
  thread.processNextEvent(true);
// addonObj is no longer null...
笑饮青盏花 2024-12-15 14:21:17

,这样做不是更好吗?

  var addonObj = -1;
AddonManager.getAddonByID("[email protected]",function(addon) {
    addonObj = addon;
});
var thread = Components.classes["@mozilla.org/thread-manager;1"].getService().currentThread;
while (addonObj != null || addonObj == -1)
  thread.processNextEvent(true);
// addonObj is no longer null...

如果没有安装插件

Would it not be better to do:

  var addonObj = -1;
AddonManager.getAddonByID("[email protected]",function(addon) {
    addonObj = addon;
});
var thread = Components.classes["@mozilla.org/thread-manager;1"].getService().currentThread;
while (addonObj != null || addonObj == -1)
  thread.processNextEvent(true);
// addonObj is no longer null...

in case the Addon is not installed?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文