Skip to content

Instantly share code, notes, and snippets.

@Duder-onomy
Last active August 29, 2015 14:20
Show Gist options
  • Save Duder-onomy/37e23c765452322bf03a to your computer and use it in GitHub Desktop.
Save Duder-onomy/37e23c765452322bf03a to your computer and use it in GitHub Desktop.
Async Image Preloader Using Promises AMD
define(['jquery'], function($) {
'use strict';
return function(urlArray) {
return $.when.apply($, urlArray.map(function(url) {
if(url) {
var $imgDeferred = new $.Deferred();
setTimeout(function() {
var imageTag = new Image();
imageTag.onload = $imgDeferred.resolve;
imageTag.onerror = $imgDeferred.reject;
imageTag.src = url;
}, 0);
return $imgDeferred.promise();
}
}));
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment