Skip to content

Instantly share code, notes, and snippets.

@fercreek
Created February 5, 2016 06:02
Show Gist options
  • Save fercreek/9550e5e13316da849cac to your computer and use it in GitHub Desktop.
Save fercreek/9550e5e13316da849cac to your computer and use it in GitHub Desktop.
import 'babelify/polyfill'
function loadImage(url) {
return new Promise((resolve, reject) => {
let image = new Image()
image.onload = function() {
resolve(image)
}
image.onerror = function() {
let message =
'Could not load image at ' + url
reject(new Error(msg))
}
image.src = url
})
}
export default loadImage
//Another file with any name
import loadImage from './load-image'
let addImg = (src) => {
let imgElement =
document.createElement("img")
imgElement.src = src
document.body.appendChild(imgElement)
}
Promise.all([
loadImage('images/example1.jpg'),
loadImage('images/example2.jpg'),
loadImage('images/example3.jpg'),
]).then((images) => {
images.forEach( img => addImg(img.src));
}).catch((error) => {
// handle error
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment