google / WebFundamentals

Former git repo for WebFundamentals on developers.google.com
Apache License 2.0
13.85k stars 2.57k forks source link

response.text has no example in the docs. #6775

Open tomachinz opened 6 years ago

tomachinz commented 6 years ago

I'm trying to process genomics data using web workers in js, so an example to process ASCII type text files would be super nice.

Suggest someone (should I submit pull request for this?) change the docs to include:

      const cacheAvailable = 'caches' in self;
      const options = {
        headers: {
          'Content-Type': 'text/plain'
        }
      }
      if (cacheAvailable) {
        const request = new Request('/genomes/gorilla-chromosome2a.txt');
        const textBlob = new Blob([data], {type: 'text/plain'});
        const stringResponse = new Response(textBlob).text;
        console.log("[stringResponse] " + stringResponse);
      } else {
        console.log("No caches!");
      }
      postMessage(stringResponse); // its inside a web worker
tomachinz commented 6 years ago

But my code above must be wrong.

If I set stringResponse as just const stringResponse = new Response(textBlob); then I'm seeing this error:

[stringResponse] [object Response] AminoSee.js:73 [downloader] ERROR: ReferenceError: stringResponse is not defined

But if I use the .text property: const stringResponse = new Response(textBlob).text; according to the spec (I'm hoping to get a string) I can't get my head around at https://developers.google.com/web/fundamentals/instant-and-offline/web-storage/cache-api I get this when I post it back to the web worker parent:

[stringResponse] function text() { [native code] }

If I wrap () at the end for const stringResponse = new Response(textBlob).text();

then i get: [downloader] ERROR: ReferenceError: stringResponse is not defined , undefined in my parent. So yeah I'm lost in the matrix presently.

kaycebasques commented 5 years ago

What doc are you referring to?