How to extract data from Promise returned in function call in array

1.4k Views Asked by At

I'm trying to convert nested array of images into base64 use RNFS and it is returning me a promise and response is in Promise,Can you help me how to just get string instead of Promise.Thanks here is the ss enter image description here

Here is my code

const items = order?.items?.map(i => {
        return {
          ...i,
          images: i.images.map(j => {
            let response = this.getBase64(j.path);
            return response;
          }),
        };
      });

 async getBase64(file) {
    return await RNFS.readFile(file, 'base64');
  }
1

There are 1 best solutions below

1
On BEST ANSWER

Use the promise api to deal with your list of promises.

const promises = images.map(j => getBase64(j.path));
const imageResults = await Promise.all(promises); // wait for the promises to resolve.
// imageResults is now a list of whatever RNFS.readFile returns.