- 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!
Great article! Would love to see this on a CDN somewhere. This wouldn’t be a bad way to go for smaller projects that don’t need all the bells and whistles of a full blown build system.
asyncas default for you js scripts a bit risky. In your example, you might end up parsing “main.js” earlier that “highlighter.js” which might lead to ReferenceErrors thrown around.