Skip to content

Instantly share code, notes, and snippets.

@ssube
Last active October 2, 2015 18:13
Show Gist options
  • Save ssube/8f442f16ed395d442c30 to your computer and use it in GitHub Desktop.
Save ssube/8f442f16ed395d442c30 to your computer and use it in GitHub Desktop.
JS polyfill loader
class Features {
static ensure(...features) {
let requires = features.filter(it => it.check()).map(it => it.polyfill);
return new Promise((res, rej) => {
require(requires, (polyfills) => {
try {
features.forEach((it, idx) => {
it.load(polyfills[idx]);
});
res();
} catch (e) {
rej(e);
}
});
});
}
}
Features.Promise = {
check: function () {
return !!window.Promise;
},
load: function (bluebird) {
window.Promise = bluebird;
},
polyfill: 'bluebird'
};
Features.fetch = {
check: function() {
return !!window.fetch;
},
load: function (fetch) {
window.fetch = fetch;
},
polyfill: 'fetch'
};
Features.ensure(Features.Promise, Features.fetch).then(run);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment