0

I've got an array of blobs responseArray. I need to get base64 url value of each blob and push in into array.

let finalScreenshotsArray = []

      responseArray.map(blob=> {
        var reader = new FileReader();
        reader.readAsDataURL(blob);
        reader.onloadend = function() {
          let base64data = reader.result;
          finalScreenshotsArray.push(base64data)


        }
      })

However, the value of base64data cannnot be accessed outside onloadend.

EDIT: I have read title of other question, that was linked to it and it's helpful, but doesn't address to my question

Konstantin
  • 129
  • 4
  • 16
  • I still don't understand how it is connected – Konstantin Jun 10 '19 at 09:48
  • I'm confused; what's wrong with the code you have? `finalScreenshotsArray` is in a wider scope than `base64data`, but you're pushing that data inot the array in the load ended event. So once those are pushed, you can certainly access that data from within the array. Are you perhaps trying to access it before the load ended event even fires? Because of course you can't do that; the data hasn't been read yet! So you need to wait until all of them have finished before trying to access it. (One way of doing that: wrap the event handler in a Promise, return it from the map, and use Promise.all.) – IceMetalPunk Jun 10 '19 at 19:15

0 Answers0