Re: [ServiceWorker] Fetch API respondWith's implicit RETURN (#844)

Jake, in a nutshell it does what it says on the tin "Best Endeavour". I haven't put any request filtering logic in yet but I was impressed was someone else's idea of trying for a network fetch but if it took longer than Xms then respond with a cached copy (if available).

As a training exercise (Promises, Fetch API) I decided to code a solution that would start 3 "threads" in motion per fetch: -

1) Check cache for a hit and retrieve it
2) Fetch from network Request URL, if error go to cache-hit
3) Start off a timer that if gets in first will respond with a cache-hit

It is also makes sure to update the cache even after the request has been responded to with a cache-hit. So, to me, the cache should be as up to date as possible.

Personally I'm more interested in seeing what I can achieve with Google Map tiles and KML caching but I needed training and that's where I'm up to. Below is the code: -

FWIW, I have to admit to being ambivalent about where this language is going :-( You were right about the CATCH forcing a resolve but for the life of me WHY? The reject() method was called the answer is settled-rejected not some rubbish "Oh he didn't let the 'exception?' bubble up so maybe he really meant RESOLVE'. Is there an ELSE on the way?  Why are you mandating an ELSE when we don't care?

And at the risk of over-sharing with people who wouldn't know me if I stood up in their corn flakes, the vomiting has turned into a lumbar punch, cat scan, chest-x-ray, 2 intravenous antibiotic drips and a saline drip and more blood tests than I can count :-( Money's on meningitis but we have to wait. As a rule don't jump into still water after a Perth summer unless you hold your nose.

 'use strict';

 function optimusConor(event)
 {           
  const   MAXDETENT   = 300,
    NOCACHE     = {cache : "no-cache"},
    WORKCACHE   = "FAC$CACHE"
    ;

  var     timerId     = 0;
  var  ioResolve   , 
    ioReject
    ;

  var  pong  = 
    new Promise(function(res, rej){
        ioResolve = res;
        ioReject  = rej;
       });

  event.respondWith(pong);

  function wait(ms){
   return new Promise(
    function(resolve){
     timerId = setTimeout(function(){resolve()}, ms);
    });
  }

  var inBuff =
   new Promise(
    function(resolve, reject){  
     caches.match(event.request.clone())
        .then(response => response === undefined ? reject() : resolve(response));
    });

  event.waitUntil(new Promise(
   function(allDone){
    fetch(event.request.clone(), NOCACHE)
     .then(response =>
      {
       if (response.ok) {
        clearTimeout(timerId);
        ioResolve.call(null, response.clone());
        caches.open(WORKCACHE).then(cache => {
         cache.put(event.request.clone(), response);
        })
       }
       allDone();
      })
     .catch(function(error) {
       allDone();
       inBuff.then(response => ioResolve.call(null, response))
        .catch(()=>{ioReject.call(null, error)});
        })
    ;

    wait(MAXDETENT)
     .then(inBuff
       .then(response => ioResolve.call(null, response))
       .catch(()=>{})
      );
     }));
 }

 self.addEventListener('install', function(event) {
   self.skipWaiting();
   console.log('Installed', event);
 });

 self.addEventListener('activate', function(event) {
   console.log('Activated', event);
 });

 self.addEventListener('fetch', optimusConor);

---
Reply to this email directly or view it on GitHub:
https://github.com/slightlyoff/ServiceWorker/issues/844#issuecomment-195939327

Received on Sunday, 13 March 2016 11:29:17 UTC