-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback.js
27 lines (21 loc) · 848 Bytes
/
callback.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
var count = 0;
module.exports = function (url, options, callback) {
if (!callback) {
callback = options;
options = {};
}
var object = options.object || window;
var key = options.key || 'a' + count++;
var parameter = 'parameter' in options ? options.parameter : 'callback';
var script = document.createElement('script');
script.src = parameter ? (url + (~url.indexOf('?') ? '&' : '?') + parameter + '=' + key) : url; // eslint-disable-line no-implicit-coercion
script.onerror = function () { // eslint-disable-line unicorn/prefer-add-event-listener
delete object[key];
callback(new Error());
};
object[key] = function (response) {
delete object[key];
callback(null, response);
};
document.head.removeChild(document.head.appendChild(script)); // eslint-disable-line unicorn/prefer-node-append
};