Skip to content

Instantly share code, notes, and snippets.

@HenrikJoreteg
Created February 16, 2016 17:31
Show Gist options
  • Save HenrikJoreteg/be9a5672b35c6c13a552 to your computer and use it in GitHub Desktop.
Save HenrikJoreteg/be9a5672b35c6c13a552 to your computer and use it in GitHub Desktop.
Polyfill module with callback. Original idea credit: Ryan Florence
export default (cb) => {
ensurePromise(() => {
ensureFetch(cb)
})
}
const ensurePromise = (cb) => {
if (typeof Promise === 'undefined') {
require.ensure([], (require) => {
require('imports?this=>window!es6-promise')
cb()
})
} else {
cb()
}
}
const ensureFetch = (cb) => {
if (typeof fetch === 'undefined') {
require.ensure([], (require) => {
require('whatwg-fetch')
cb()
})
} else {
cb()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment