Random behavior of an array buffer

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

It looks like sometimes arrayBuffer () works and sometimes it doesn't. Doesn't work, I mean, sometimes it returns an empty array. It doesn't work most of the time.

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 this a bug?

Thanks, Nicolas

+3


source to share





All Articles