Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

102
Views
async await promise vue composition api console.log is correct but not in vue template

I have a multi-file input object "files" and I want to generate preview blob. The previews object is correct, however doing files.value.map does not work. I want to attach the blob to the "files" object. What am I missing?

const files = ref([])
const previews = ref([])

const toBlob = async(file) => {
  const buffer = await file.arrayBuffer()
  const blob = new Blob([buffer])
  const srcBlob = URL.createObjectURL(blob)

  return srcBlob
}

watch(files, async() => {
  previews.value = await Promise.all(
    files.value.map((file) => toBlob(file))
  )

  await Promise.all(files.value.map(async(file) => {
     console.log(file)
     file.preview = await toBlob(file)
  }))
})

 return {
   files,
   previews
 }

This is blank in the vue template. console.log is correct however.

<span v-for="file in files" :key="file">{{file.preview}}</span>

This is correct, showing the preview blob:

<span v-for="preview in previews" :key="preview">{{preview}}</span>
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

By passing an async function to files.value.map, you're getting an array of Promises. You can't await this array directly, because you can only await a Promise.

Instead, you can use Promise.all to turn an array of Promises into a Promise of an array:

  await Promise.all(files.value.map(async(file) => {
    console.log(file)
    file.preview = await toBlob(file)
  }))
about 4 years ago · Juan Pablo Isaza Report

0

I'm pretty sure the assignment of file.preview breaks the reactivity. You could try this in Vue3:

let { preview } = toRefs(file)
preview.value = await toBlob(file)

References:

  • https://v3.vuejs.org/guide/reactivity-fundamentals.html#destructuring-reactive-state
about 4 years ago · Juan Pablo Isaza Report

0

It doesn't look like you actually need to attach a .preview property to the File object, since you already store them in previews[], so just remove that code:

watch(files, async() => {
  previews.value = await Promise.all(
    files.value.map((file) => toBlob(file))
  )

  // xxx: remove
  //await Promise.all(files.value.map(async(file) => {
  //   console.log(file)
  //   file.preview = await toBlob(file)
  //}))
})

The object URLs would not be rendered as images with string interpolation (i.e., between curly bracket pairs). That is, {{previewObjectUrl}} would only render the URL as a string.

To render the blobs, the object URLs must be bound to an <img>.src:

<img v-for="preview in previews" :key="preview" :src="preview">

demo

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!