- cache results (though that would be easy)
- provide a module/object back
- do AJAX calls (though a XHR-to-Promise shim is available, or you can use fetch)
- … or anything else advanced
Here is the tiny “loader” in all of its glory:
load object is created with
img functions which accept a URL to load. Each function returns a Promise and the
onerror event of the resource’s tag triggers
reject for the promise.
Promise.all collects the resources to be loaded and
then triggers upon successful load of all resources,
catch if any of them fail.
I have to stress that this is meant to be a very, very simple “loader”; please save the comments about how it doesn’t have bells and whistles that other loaders have. I love how awesome the Promise API makes async and resource loading management, as does the ServiceWorker API and fetch API. Do yourself a favor and check out these awesome APIs!