我需要帮助在Javascript中使用双重承诺

我需要帮助在Javascript中使用双重承诺,javascript,arrays,reactjs,asynchronous,promise,Javascript,Arrays,Reactjs,Asynchronous,Promise,这是我试过的代码 // To get base64 code of file const toBase64 = file => new Promise((resolve, reject) => { const reader = new FileReader(); reader.readAsDataURL(file); reader.onloaded = () => resolve(reader.result.replace(/^data:.+;b

这是我试过的代码

  // To get base64 code of file
  const toBase64 = file => new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onloaded = () => resolve(reader.result.replace(/^data:.+;base64,/, ''));
    reader.onerror = error => reject(error);
  })

  // To make an array of files
  const getAttachments = async files => {
    let documents;
    try {
      documents = files.map(async file => {
        let base64 = await toBase64(file)
        return {
          doc: base64,
          documentName: file.name,
          documentType: file.type
        }
      })
    } catch {
      console.error('Failed to get files as base64')
    }
    return Promise.resolve(documents)
  }
我试着通过使用上面的两个函数得到一个对象数组。 如下所示

getAttachments(Array.from(event.target.files)).then(documents => {
  console.info(documents)
}
但结果是

我想知道怎样才能得到我想要的。
谢谢。

尝试使用
wait
关键字返回已解析承诺的数组,而不是返回承诺数组

试试这个

const getAttachments=异步文件=>{
出租文件;
试一试{
documents=files.map(异步文件=>{
让base64=等待toBase64(文件)
返回{
文件编号:base64,
documentName:file.name,
documentType:file.type
}
})
返回等待承诺。所有(文件);
}抓住{
console.error('无法将文件作为base64'获取)
}
}

documents=wait Promise.all(files.map)(async file=>
etc感谢您的评论,但控制台中没有任何内容被注销。您可能做错了…下面的答案如何?行了吗?它也没有注销任何内容。您好@Bravo,谢谢您的时间。我可以看到问题所在。我已在
中将
reader.onload
更改为
reader.onload
toBase64()
方法,它现在正在工作。感谢您的回答,但在我运行此函数时,控制台中没有注销。
getAttachments(files)。然后(documents=>{console.info(documents)}
也许
文件
是空数组@baymax88,或者你被过滤掉了
信息
日志…试试
控制台.日志
而不是
控制台.信息
顺便说一下
返回等待承诺.所有(文档);
只要做
返回承诺.所有(文档);
我添加了
控制台.日志(文件)
之前让文档;
并且它正在注销
文件
,但是没有任何结果被注销。在
返回
输出@baymax88之前,
控制台.log(documents)
做什么