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