简体   繁体   English

嵌套获取以在服务工作者中进行响应

[英]nested fetch for a response in a service-worker

I'm new to service-worker. 我是服务人员的新手。 I'm following a training of Mobile Web Specialist given by Udacity and I'm using google-chrome for that. 我正在接受Udacity提供的有关移动网络专家的培训,并且正在为此使用google-chrome。 I want to fetch for a response from the network, and if it returns 404 as a status I fetch for another response from the network as well. 我想从网络获取响应,如果它返回404作为状态,我也要从网络获取另一个响应。 This is a code to fetch from the network only once. 这是一个只能从网络获取一次的代码。 This code works perfectly: 这段代码可以完美地工作:

self.addEventListener('fetch', function(event) {
  event.respondWith(
    fetch(event.request).then(function(response) {
      if (response.status === 404) {
        return new Response("Whoops, not found");
      }
      return response;
    }).catch(function() {
      return new Response("Uh oh, that totally failed!");
    })
  );
});

I did some updates on this code by throwing an error after getting response.status === 404 and manage it the same way in a try/catch . 我在获取response.status === 404后抛出错误,对代码进行了一些更新,并通过try/catch进行相同的管理。 The updated code is below: 更新后的代码如下:

self.addEventListener('fetch', function(event) {
 try {
  event.respondWith(
    fetch(event.request).then(function(response) {
      if (response.status === 404) {
        throw (Error);
      }
      return response;
    }).catch(function() {
      return new Response("Uh oh, that totally failed!");
    })
  );
 } catch (Error) {
   event.respondWith(
    fetch('/imgs/dr-evil.gif').then(function(response) {
      if (response.status === 404) {
        return new Response('couldn\'t fetch twice');
      }
      return response;
    }).catch(function() {
      return new Response("Uh oh, that totally failed twice!");
    })
  );
 }
});

I know there is a better way to do a nested fetch using the service-worker, but I want to know what I did wrong here. 我知道有一个更好的方法可以使用service-worker进行嵌套获取,但是我想知道我在这里做错了什么。

I've not run this so it's possible it needs some adjustments, but try something like this. 我尚未执行此操作,因此可能需要进行一些调整,但请尝试类似的操作。 The problem with your current code is that the first fetch promise chain always resolves to a Response. 您当前代码的问题在于,第一个获取承诺链始终解析为响应。 Either in the first then or in the first catch , where you return a response of "Uh oh, that totally failed!" 在第一个then的第一个catch ,或者在第一个catch ,您将返回"Uh oh, that totally failed!"的响应"Uh oh, that totally failed!" . The event.respondWith takes that response and happily goes along it's way. event.respondWith接受该响应并愉快地进行。

The outer try/catch exists in a synchronous space, where as the fetch kicks off an asynchronous chain, so there will be no way for your code to reach the outer catch since it's not in the execution context for the fetch. 外部try/catch存在于同步空间中,在该空间中,由于获取启动了异步链,因此您的代码将无法到达外部catch,因为它不在获取的执行上下文中。

If the compatability is the same for both service worker and async/await (I don't know) you might want to take a look at that as it would be a much friendlier way to structure your code. 如果服务工作者和异步/等待的兼容性都是相同的(我不知道),您可能需要看看一下,因为这是构建代码的更友好的方式。

self.addEventListener('fetch', function(event) {
    event.respondWith(
        fetch(event.request).then(function(response) {
            if (response.status === 404) {
                throw (Error);
            }
            return response;
        }).catch(function() {
            return fetch('/imgs/dr-evil.gif').then(function(response) {
                if (response.status === 404) {
                    throw (Error);
                }
                return response;
            })
        }).catch(function() {
            return new Response("Uh oh, that totally failed twice!");
        })
    ); 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM