-1

I am trying to get the value returned by a promise however, I am constantly getting a Promise object:

const readImageToBase64 = file => new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => {
        const result = reader.result.toString();
        resolve(result.replace("data:image/jpeg;base64,", ""))
    }
    reader.onerror = error => reject(error);
});

const imageToBase64 = async (img) => {
   const file = img;
   const result = await readImageToBase64(file).then(response => {
        return response;
   });
   return result;
}

And inside another funciton I have:

const base64 = images.map(img => {
        return imageToBase64(img)
    })

However when I do console.log(base64) I get an Array containing promises rather than strings.

ashes999
  • 1,234
  • 1
  • 12
  • 36

1 Answers1

1

You need to use Promise.all to resolve your array of promises:

const result = await Promise.all(base64);
Nicolas Menettrier
  • 1,647
  • 1
  • 13
  • 28