有没有一种好的方法来实现Promise.all一个具有属性的对象数组?

314*_*ple 5 javascript asynchronous promise

如果我有很多承诺,我可以简单Promise.all地等待所有承诺。

但是,当我有一个对象数组时,每个对象都具有应有的某些属性,有没有好的方法来处理它?

例:

const files=urlOfFiles.map(url=>({
  data: fetch(url).then(r=>r.blob()),
  name: url.split('/').pop()
}))
//what to do here to convert each file.data to blob?
//like Promise.all(files,'data') or something else
Run Code Online (Sandbox Code Playgroud)

Tul*_*lir 7

您可以将数据映射到解析为对象的promise数组,而不是将数据映射到对象数组:

const promises = urlOfFiles
    .map(url => fetch(url)
        // r.blob() returns a promise, so resolve that first.
        .then(r => r.blob())
        // Wrap object in parentheses to tell the parser that it is an
        // object literal rather than a function body.
        .then(blob => ({
            data: blob,
            name: url.split('/').pop()
        })))

Promise.all(promises).then(files => /* Use fetched files */)
Run Code Online (Sandbox Code Playgroud)