promise-create-script
Version:
Creates script and returns a promise.
21 lines (20 loc) • 600 B
JavaScript
/**
* Creates the script element and provides a callback via onload
* @param {[type]} el [description]
* @param {[type]} uri [description]
* @return {[type]} [description]
*/
module.exports = function (el, uri) {
return new Promise(function (resolve, reject) {
var script = document.createElement('script')
script.src = uri
script.type = 'text/javascript'
script.onload = function () {
resolve(script)
}
script.onerror = function (err) {
reject(new Error('Script "' + err.target.src + '" is not accessible.'))
}
el.appendChild(script)
})
}