From Innocent Earthworm, 1 Month ago, written in JavaScript.
  1. // Variables used by fetchDaily:
  2. var todayStr = null;
  3. var manifestURL = null;
  4. var manifestList = null;
  5.  
  6. // Read the manifest (which should already be in the cache)
  7. // and set the global variable manifestList to its split contents.
  8. function readManifest() {
  9.     console.log("fetching " + manifestURL);
  10.  
  11.     fetch(manifestURL).then(function(response) {
  12.         if (!response.ok) {
  13.             // This throw matters if there's no catch after then(function(txt))
  14.             // If there is a catch there, this throw is ignored, not caught
  15.             // by that catch.
  16.             throw("Couldn't fetch " + manifestURL + ": status "
  17.                   + response.status + " " + response.statusText);
  18.         }
  19.         return response.text();
  20.     }).then(function(txt) {
  21.         // console.log("Read text: " + txt);
  22.         manifestList = txt.split(/\r?\n/);
  23.         console.log("Split into " + manifestList.length + " lines");
  24.         return manifestList;
  25.     })
  26.     .catch(err => console.log("Error fetching manifest: " + err));
  27.  
  28.     // Notreached
  29. }
  30.  
  31. function fetchDaily() {
  32.     event.preventDefault();
  33.     if (!todayStr)
  34.         todayStr = formatDate(new Date());
  35.     console.log("No MANIFEST yet: fetching it");
  36.     manifestURL = feedTop + '/' + todayStr + '/MANIFEST'
  37.  
  38.     // .then takes two arguments, for success and failure;
  39.     // it returns a Promise, which will run either the success or
  40.     // failure function after completion of the function before it.
  41.     // Presumably calling that function with the return value of
  42.     // the preceding function, but it's not like the documentation
  43.     // bothers to go into such niggling details.
  44.  
  45.     // This should fetch and cache the manifest:
  46.     caches.open(manifestURL).then(function(cache) {
  47.         readManifest().then(function(response) {
  48.             if (!manifestList)
  49.                 console.log("I guess reading the manifest failed");
  50.             else
  51.                 console.log("Inside the promise, the manifest has "
  52.                             + manifestList.length + " lines");
  53.             return response;
  54.         })
  55.     });
  56. }
  57.  
captcha