forked from ember-engines/ember-asset-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js.js
30 lines (26 loc) · 792 Bytes
/
js.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import RSVP from 'rsvp';
import { createLoadElement, nodeLoader } from './utilities';
/**
* Default loader function for JS assets. Loads them by inserting a script tag
* with onload/onerror handlers that correspond to the resolve/reject callbacks
* of the return Promise.
*
* @param {String} uri
* @return {Promise}
*/
export default nodeLoader(function js(uri) {
return new RSVP.Promise((resolve, reject) => {
if (document.querySelector(`script[src="${uri}"]`)) {
return resolve();
}
const script = createLoadElement('script', resolve, function(error) {
if (this.parentNode) {
this.parentNode.removeChild(this);
}
reject(error);
});
script.src = uri;
script.async = false;
document.head.appendChild(script);
});
});