Created
July 13, 2014 14:53
-
-
Save ghoullier/6e34127946eb8280f5c2 to your computer and use it in GitHub Desktop.
Load Image using promise
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
;(function(root, doc) { | |
var log = console.log.bind(console) | |
, error = console.error.bind(console) | |
function trace(args) { | |
log(args.callee.name, args) | |
} | |
function load(url) { | |
return new Promise(function(resolve, reject) { | |
var image = new Image() | |
image.src = url | |
image.addEventListener('load', onLoad); | |
image.addEventListener('error', onError); | |
function onLoad(event) { | |
trace(arguments) | |
resolve(image, event) | |
} | |
function onError(error) { | |
trace(arguments) | |
reject(error) | |
} | |
}) | |
} | |
}(this, this.document)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment