3

I want to fetch directly an arrayBuffer using the fetch api (https://fetch.spec.whatwg.org/). Once the data is returned, I want to use the array buffer.

It appears that sometimes arrayBuffer() works and sometimes it doesn't. By doesn't work I mean sometimes it returns an empty array. Most of the time it does not work.

fetch(url).then(function(response) {
      response.arrayBuffer().then(function(buffer){
            results[i] = buffer;
        });
});

If I call blob() and convert it to an array buffer via FileReader it always works.

fetch(url).then(function(response) {
      response.blob().then(function(buffer){
            results[i] = buffer;
        });
});

...

var myReader = new FileReader();
myReader.addEventListener("loadend", function(e){
    // ALWAYS GOOD
    var byteArray = new Uint8Array(e.srcElement.result);
});

myReader.readAsArrayBuffer(results[0]);

And a live demo: http://codepen.io/nicolasrannou/pen/OVLyjX

Am I doing something wrong or is it a bug?

Thanks, Nicolas

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Nicolas
  • 2,191
  • 3
  • 29
  • 49
  • What environment is this? I get fetch is not defined on both Chrome and FF. – Musa Apr 17 '15 at 13:57
  • You should use chrome canary as those are still experimental features! Thanks – Nicolas Apr 20 '15 at 07:12
  • Please provide your full code. What is `results`, what is `i`, where do you try to use it? It seems your promise usage is wrong. – Bergi Sep 08 '15 at 13:41
  • you aare right the promise probably seems wrong. Anyway, didn't go any further with Fetch as it doesn't report progress of a download. – Nicolas Sep 11 '15 at 10:09
  • So if you don't want this answered any more, delete it, otherwise please supply additional information. – Bergi Sep 12 '15 at 20:08

0 Answers0