javascript,在ChromeWeb应用程序中创建窗口,如何包括成功调用新窗口函数的承诺?

javascript,在ChromeWeb应用程序中创建窗口,如何包括成功调用新窗口函数的承诺?,javascript,angularjs,promise,google-chrome-app,Javascript,Angularjs,Promise,Google Chrome App,我想创建一个窗口,当它被创建时,运行一个已经预加载的函数。我必须等待窗口创建,否则函数不可用并失败。我要说我可以答应你 怎么做?这是我尝试的代码 one().then(function() { new_panel.contentWindow.load_content(something); }) function one() { var promise = chrome.app.window.create('empty.html', { id: 'protein_pane

我想创建一个窗口,当它被创建时,运行一个已经预加载的函数。我必须等待窗口创建,否则函数不可用并失败。我要说我可以答应你

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

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:uncaughttypeerror:无法读取未定义的属性'then'

更新:稍微修改了代码,仍然不起作用,我使用angular的promise,因为我正在使用它

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:或者其他方法来做同样的事情(也不起作用)


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

您希望从
one()
同步返回承诺,并在创建新窗口后使用新窗口解决该承诺:

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;
}

它到底是如何“抱怨”的?
chrome.app.window.create(
的返回值是多少?换句话说,
one()
的返回值是多少?uncaughttypeerror:无法读取未定义的属性“then”,而promise的值是未定义的<代码>承诺不是承诺,而是未定义的承诺。是的,这就是问题所在。我如何表达一个类似承诺的结构,以便在加载该函数时能够调用该函数?代码中的promise变量是我想要尝试的,老实说,我不知道怎么做。主要是因为create()函数不是我的,所以我无法在其中添加要返回的值。我不知道该怎么办你能修一下缩进吗?此外,您还应该查看Chrome API文档是否支持承诺(我猜在您不传递回调时,它们会支持承诺),否则请查看参数createdWindow在中的作用。然后(函数(createdWindow??我的意思是,承诺是必需的,还是只是访问createdWindow的一种方式(然后可以交换)@杰拉德:承诺没有必要按照正确的顺序解决。我只是为了方便起见添加了它,这样你就可以访问createdWindow,如果你愿意的话。非常感谢,我已经实现了它,并且工作得非常完美^_^
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;
}