【问题标题】:javascript, create window in chrome web app, how to include a promise for successfully calling a function of the new window?javascript,在 chrome web 应用程序中创建窗口,如何包含成功调用新窗口功能的承诺?
【发布时间】:2015-09-10 09:38:21
【问题描述】:

我想创建一个窗口,当它被创建时,运行一个已经预加载的函数。我必须等到创建窗口,否则该功能不可用并失败。我会说我可以通过一个承诺来做到这一点。

怎么做?这是我尝试做的代码

one().then(function() {
  new_panel.contentWindow.load_content(something);
})

function one() {
  var promise = chrome.app.window.create('empty.html', 
  {
    id: 'protein_panel',
    outerBounds: {
      width: 300,
      height: 800,
    },
  },
  function(createdWindow) {
    createdWindow.contentWindow.load_content = function(data) {
      createdWindow.contentWindow.document.getElementById('loading_content').appendChild(data);
    }
    new_panel = createdWindow;
  });

  return promise;
}

它抱怨 one() 函数的 then:Uncaught TypeError: Cannot read property 'then' of undefined

更新:稍微修改了代码,仍然无法正常工作,自从我使用它以来,我已经使用了 Angular 的承诺

one().then(function() {
  new_panel.contentWindow.load_content(something);
})

function one() {
  return chrome.app.window.create('empty.html', 
  {
    id: 'protein_panel',
    outerBounds: {
      width: 300,
      height: 800,
    },
  },
  function(createdWindow) {
    createdWindow.contentWindow.load_content = function(data) {
      createdWindow.contentWindow.document.getElementById('loading_content').appendChild(data);
    }
    new_panel = createdWindow;

    var deferred = $q.defer();
    deferred.resolve();
    console.log(deferred.promise)
    return deferred.promise;
  });
}

理论上,deferred.promise 应该只在它被解析时返回,但它就像 then() 函数在实际发生之前执行。为什么?

UPDATE2: 或另一种方法来做同样的事情(也不起作用)

one();

function one() {
  chrome.app.window.create('empty.html', 
  {
    id: 'protein_panel',
    outerBounds: {
      width: 300,
      height: 800,
    },
  },
  function(createdWindow) {
    createdWindow.contentWindow.load_content = function(data) {
      createdWindow.contentWindow.document.getElementById('loading_content').appendChild(data);
    }
    new_panel = createdWindow;

    var deferred = $q.defer();
    deferred.resolve();
    console.log('first '+deferred.promise)
    return deferred.promise;
  }).then(function() {
    console.log('second')
    new_panel.contentWindow.load_content(something);
  });
}

日志“second”在“first”之前打印。代码有什么问题吗?

【问题讨论】:

  • 它究竟是如何“抱怨”的? chrome.app.window.create(...) 的返回值是多少?也就是说one()的返回值是多少?
  • Uncaught TypeError: Cannot read property 'then' of undefined,promise 的值是 undefined
  • 你去。 promise 不是 Promise 而是 undefined
  • 是的,是的,这就是问题所在。我怎样才能清楚地表达一个类似承诺的结构,以便能够在加载该函数时调用该函数?代码的 promise 变量是我想要的尝试,老实说我不知道​​该怎么做。主要是因为 create() 函数不是我的,我不能在那里添加要返回的值。不知道在这里做什么
  • 你能修正你的缩进吗?此外,您应该查看 Chrome API 文档,它们是否支持 Promise(我猜当您不传递回调时它们会支持),否则请参阅 How do I convert an existing callback API to promises?

标签: javascript angularjs promise google-chrome-app


【解决方案1】:

您想从one() 同步返回一个promise,并在新窗口创建后解决该promise:

one().then(function(createdWindow) {
  // Window has been created
  createdWindow.contentWindow.load_content(something);
})

function one() {
  // create a promise
  var deferred = $q.defer();

  chrome.app.window.create('empty.html', 
  {
    id: 'protein_panel',
    outerBounds: {
      width: 300,
      height: 800,
    },
  },
  function(createdWindow) {
     // resolve our promise and pass the window to the `then` block
     deferred.resolve(createdWindow)
  });

  // synchronously return a promise
  return deferred.promise;
}

【讨论】:

  • 参数 createdWindow 在 .then(function(createdWindow ?? )
  • @Gerard:承诺没有必要以正确的顺序解决。我只是为了方便而添加它,以便您可以根据需要访问 createdWindow。
  • 非常感谢,我已经实现并且完美运行^_^
猜你喜欢
  • 2017-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-17
  • 1970-01-01
  • 2015-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多