0

I want to create a window and when it is created, run a function that has been pre-loaded. I do have to wait until the window is created, otherwise the function is not available and fails. And I'd say I can do it with a promise.

How to do it? Here there's the code with my attempt to do it

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

It complains about the then of the one() function: Uncaught TypeError: Cannot read property 'then' of undefined

UPDATE: Slightly modified code, still doesn't work, I've used the promise from angular since I'm using it

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

In theory, deferred.promise should only be returned when it is resolved, but it is like the then() function is executed before it actually happens. Why?

UPDATE2: or another way to do the same (doesn't work either)

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

The log 'second' is printed before 'first'. Is there anything wrong with the code?

GWorking
  • 4,011
  • 10
  • 49
  • 90
  • How exactly does it “complain”? What’s the return value of `chrome.app.window.create(`…`)`? In other words, what’s the return value of `one()`? – Sebastian Simon Sep 10 '15 at 09:45
  • Uncaught TypeError: Cannot read property 'then' of undefined, and the value of promise is undefined – GWorking Sep 10 '15 at 09:46
  • There you go. `promise` is not a Promise but `undefined`. – Sebastian Simon Sep 10 '15 at 09:49
  • Yes yes, that is the problem and the question. How can I articulate a promise-like structure to be able to call the function WHEN this has been loaded? The promise variable of the code is my wannabe attempt, honestly I don't know how to do it. Mostly because the create() function is not mine, I cannot add there a value to be returned. Don't know what to do here – GWorking Sep 10 '15 at 10:19
  • Can you fix your indentation, please? Also you should have a look at the Chrome API docs whether they do support promises (I guess they do when you don't pass a callback), otherwise see [How do I convert an existing callback API to promises?](http://stackoverflow.com/q/22519784/1048572) – Bergi Sep 10 '15 at 13:39

1 Answers1

2

You want to synchronously return a promise from one() and resolve the promise with the new window once it has been created:

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;
}
dthareja
  • 146
  • 1
  • 4
  • What is the role of the argument createdWindow in .then(function(createdWindow ?? I mean, is it necessary for the promise, or it is only a way to access createdWindow (and then could be exchanged) – GWorking Sep 13 '15 at 08:13
  • @Gerard: It's not necessary for the promise to resolve in the correct order. I just added it as a convenience so you could access the createdWindow if you wanted. – dthareja Sep 14 '15 at 16:29
  • Thanks a lot, I have implemented it and works perfectly ^_^ – GWorking Sep 15 '15 at 07:55