I can't quite work out why my async map fetch function returns an array of undefined values or "Promise { }" no matter what I do. My getImageRef function works (I've tried to simplify it by stripping some bits out) but how do I get it to return values to the Promise.all in formatBooks?
const inventory = [
{
"isbn":"foo",
"title":"bar",
"imageURL":"http://web.com/image.jpg"
}
]
const getImageRef = async book => { //this part works on its own
await fetch(book.imageURL)
.then(res => res.buffer())
.then(buffer => client.assets.upload('image', buffer))
.then(assetDocument => {
let book.imageRef = assetDocument._id
return book
})
.catch(() => {
console.error('Error: ' + book.image)
return book
})
}
async function formatBooks (inventory) {
await Promise.all(inventory.map(async book => {
const docs = await getImageRef(book) // 'docs' returns as undefined
})
).then(function(docs) {
uploadBooks(docs)
})
}
function uploadBooks (documents) {
console.log(documents)
}
formatBooks(data)