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