admin管理员组文章数量:1242790
I want to send a $http.get if the page gets closed. Then I stumbold upon a problem where
promises can't get resolved because if this one last method returns, the page gets destroyed.
The following does not work because onbeforeunload can't resolve promises / does not wait for them:
window.onbeforeunload = function(
$http.get('/update-state?page=unloaded').then(function(){
// never called... never sent...
}
}
I know, you can use default sync HTTP Methods but the question is generally how could we sync/wait for promises to resolve here.
My idea would be something like this:
window.onbeforeunload = function(){
var ready = false;
$http.get('/update-state?page=unloaded').then(function(){
ready=true;
}
// I don't see any other opportunity to sync a promise here than this:
while(!ready) angular.noop();
// big question: did $http call the server now? Can we finally return in this method to let the browser 'exit' the page?
}
RFC
I want to send a $http.get if the page gets closed. Then I stumbold upon a problem where
promises can't get resolved because if this one last method returns, the page gets destroyed.
The following does not work because onbeforeunload can't resolve promises / does not wait for them:
window.onbeforeunload = function(
$http.get('http://someth.ing/update-state?page=unloaded').then(function(){
// never called... never sent...
}
}
I know, you can use default sync HTTP Methods but the question is generally how could we sync/wait for promises to resolve here.
My idea would be something like this:
window.onbeforeunload = function(){
var ready = false;
$http.get('http://someth.ing/update-state?page=unloaded').then(function(){
ready=true;
}
// I don't see any other opportunity to sync a promise here than this:
while(!ready) angular.noop();
// big question: did $http call the server now? Can we finally return in this method to let the browser 'exit' the page?
}
RFC
Share Improve this question asked Apr 2, 2016 at 21:52 noXinoXi 3121 gold badge2 silver badges7 bronze badges 3-
2
If you're using async Ajax in
onbeforeunload
, there are simply no guarantees and no supported ways to do that. Though it has its own issues, it is possible to send synchronous Ajax (at a risk of sacrificing the user experience). Usually, the better solution is to solve whatever problem you're trying to solve a different way. Some people use the close of a webSocket connection as seen on the server as a notification of when the page has been closed. – jfriend00 Commented Apr 2, 2016 at 22:16 - WebSockets could be good solution. Thanks – noXi Commented Apr 2, 2016 at 22:23
- Were you able to figure it out using webSockets – Nayeem Commented May 11, 2018 at 19:53
1 Answer
Reset to default 10You have 2 problems:
- You are using a callback (event handler) intended as "last-chance", this event handler, if browsers typically treat it in a special way, and doing any kind of long-running operations here is not intended.
- Standard operation workflow will resume after this callback exits, meaning that any asynchronous operations are likely not going to be finished, especially if they are guaranteed to not be executed in the current tick (promises fall under this category, they will never be resolved synchronously by design).
So what to do to work within these limitations? You have a few options:
Avoid asynchronous code. You can do the http fetch before you get to this point, store the results and use them synchronously inside onbeforeunload.
Use ServiceWorker to continue doing work after page has been terminated. Again, I suggest creating, registring and activating worker before you get to handling onbeforeupload. During onbeforeupload handling - you want to
postMessage
to your worker, asking it to do some work for you. This option es with 2 caveats:- ServiceWorker is not a released standard, it's still a draft, expect API changes, browser support issues and, indeed, potential scrapping of the API all together.
- Worker has no direct access to your page, you have to be aware of that, what what it means. For example - no window object, no libraries loaded on your page, separate script for the worker, exchange of data via explicit messages only - to name a few.
本文标签: javascriptwait for promises in onbeforeunloadStack Overflow
版权声明:本文标题:javascript - wait for promises in onbeforeunload - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740111434a2226282.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论