简体   繁体   中英

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

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 在此处输入图像描述

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');
  }

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.

Have you tried to make the function inside the map become synchronous?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM