Skip to content

Instantly share code, notes, and snippets.

@dimensi
Last active July 12, 2018 22:08
Show Gist options
  • Select an option

  • Save dimensi/485a5c28fd76b2930eeaba56fc75a42a to your computer and use it in GitHub Desktop.

Select an option

Save dimensi/485a5c28fd76b2930eeaba56fc75a42a to your computer and use it in GitHub Desktop.
export const waitImage = ref =>
new Promise((resolve, reject) => {
ref.onload = (event) => { resolve(event); ref.onload = null };
ref.onerror = (event) => { reject(event); ref.onerror = null };
});
const createImage = str => Object.assign(document.createElement('img'), { src: str });
const waitImages = arrStr => Promise.all(arrStr.map(str => waitImage(createImage(str))));
const images = Array.from({ length: 10 }, (_, index) => `https://source.unsplash.com/random?v=${index}`)
export default {
data: () => ({ isLoading: true }),
async mounted() {
await waitImages(images)
this.isLoading = false;
}
}
<template>
<img v-for="img in imgs" :src="img.src" :key="img.id" ref="images"/>
</template>
<script>
const waitImage = ref =>
new Promise((resolve, reject) => {
ref.onload = (event) => { resolve(event); ref.onload = null };
ref.onerror = (event) => { reject(event); ref.onerror = null };
});
const waitImages = arrImgs => Promise.all(arrImgs.map(waitImage));
export default {
props: {
imgs: Array,
},
data: () => ({ isLoading: true }),
async mounted() {
await waitImages(this.$refs.images)
this.isLoading = false;
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment