javascriptgoogle-chromepromiseq

Open window inside promise


We are using a promise lib Q and we encounter the following issue. window.open() is blocked by browser when is called from promise

We try to open the window before the promise mechanism has started and this is working but the issue is that when a new tab is opened (and get the focus) all the browser resources are refer to the new tab and the logic is still done in the first tab which cause to bad performance.

There is other way to handle this maybe with event when the promise finished and then catch this event and open new window.

Update

What does it mean "all the browser resources are refer to the new tab" we found interesting behavior :) We are running application when user click on button, in this case we are open new tab and the focus is changed to the new tab and we are waiting to the app to start, this takes about 15 second (until the application actually running). We did the following test (which is very interesting :) ) and when we click on run application and the new tab is opens we are immediately click back to the first tab (which have the buttons) and by doing this the time is reduce to 4.5 seconds! It seems that the focus change the way that the resources is handled by the browsers.

I'm searching for alternative solutions!


Solution

  • This is because of a conflict among two policies:

    1. By specification, promises execute .then() handlers asynchronously (after the current event loop has finished).

    2. For usability and security reasons window.open() can only be called from a direct user action (during the same event loop processing that was started by a user action or within a short time afterwards from some user action).

    Because of #2, you will not be able to open a window from a .then() handler of a promise that follows the promise specification.

    The usual work-around is to open the window synchronously when the user clicks (before the promise has been resolved) and then to either fill in the content of the window you have already opened when the promise resolves or in error cases to close the window. This is not ideal, but if asynchronous operations are involved in the opening of a window, there really aren't other choices besides obtaining elevated privileges such as in a browser plugin (not from a normal web page script).

    For more detailed help on how to solve specific coding issues with implementing this work-around, you will have to add your actual code to your question and describe the specific problems with it you are running into.

    Questions asking for help with specific code must include the code you want help with.