Created
February 5, 2016 06:02
-
-
Save fercreek/9550e5e13316da849cac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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