在onbeforeunload中等待承诺 [英] wait for promises in onbeforeunload

查看:182
本文介绍了在onbeforeunload中等待承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果页面关闭,我想发送一个$ http.get.然后我对其中的问题<之以鼻
的诺言无法解决,因为如果后一种方法返回,则页面将被破坏.

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.

以下内容不起作用,因为onbeforeunload无法解析承诺/不等待它们:

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

}

我知道,您可以使用默认的同步HTTP方法,但问题通常是我们如何才能同步/等待在此处解决的诺言.

I know, you can use default sync HTTP Methods but the question is generally how could we sync/wait for promises to resolve here.

我的想法是这样的:

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

推荐答案

您有2个问题:

  • 您正在使用旨在作为最后机会"的回调(事件处理程序),如果浏览器通常以特殊方式处理此事件处理程序,则此处不打算进行任何长时间运行的操作.
  • /li>
  • 此回调退出后,标准操作工作流程将恢复,这意味着任何异步操作都可能不会完成,特别是如果保证它们不会在当前滴答中执行(承诺属于此类,则永远不会被执行).通过设计同步解决).
  • 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:

  • 避免异步代码.您可以先进行http提取,然后存储结果并在onbeforeunload内部同步使用它们.

  • Avoid asynchronous code. You can do the http fetch before you get to this point, store the results and use them synchronously inside onbeforeunload.

使用 ServiceWorker 继续进行工作页面终止后.同样,我建议在开始onbeforeupload之前创建,重新注册并激活worker.在onbeforeupload处理期间-您要 postMessage 给您的工人,要求它为您做一些工作.此选项附带两个警告:

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 comes with 2 caveats:

ServiceWorker并不是一个发布的标准,它仍然是草案,期望API发生变化,浏览器支持问题,甚至可能完全废弃该API.

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无法直接访问您的页面,您必须意识到这一点,这意味着什么.例如-没有窗口对象,您的库中未加载任何库页面,用于工作人员的单独脚本,仅通过显式消息交换数据-仅举几例.

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.

这篇关于在onbeforeunload中等待承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆