If I have an array of promises, I can simply use Promise.all
to wait for them all.
But when I have an array of objects, each of them having some properties that are promises, is there a good way to deal with it?
Example:
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
Instead of mapping the data to an array of objects, you could map it to an array of promises that resolve to objects:
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 */)
Try something like this:
const files = urlOfFiles.map(url=>
fetch(url).then(r=> ({
data: r.blob()
name: url.split('/').pop()
})
))
Promise.all(files)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With