微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从数组中函数调用返回的Promise中提取数据

如何解决如何从数组中函数调用返回的Promise中提取数据

我正在尝试使用 RNFS 将嵌套的图像数组转换为 base64,它向我返回一个承诺并且响应在 Promise 中,你能帮我如何只获取字符串而不是 Promise。谢谢 这是 ss

enter image description here

这是我的代码

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

解决方法

使用 promise api 处理您的 promise 列表。

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.
,

你有没有试过让地图里面的函数变得同步?

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。