|
23 | 23 | return 'jsonp_' + Date.now() + '_' + Math.ceil(Math.random() * 100000);
|
24 | 24 | }
|
25 | 25 |
|
26 |
| - // Known issue: Will throw 'Uncaught ReferenceError: callback_*** is not defined' error if request timeout |
| 26 | + // Known issue: Will throw 'Uncaught ReferenceError: callback_*** is not defined' |
| 27 | + // error if request timeout |
27 | 28 | function clearFunction(functionName) {
|
28 | 29 | // IE8 throws an exception when you try to delete a property on window
|
29 | 30 | // http://stackoverflow.com/a/1824228/751089
|
|
39 | 40 | document.getElementsByTagName('head')[0].removeChild(script);
|
40 | 41 | }
|
41 | 42 |
|
42 |
| - var fetchJsonp = function fetchJsonp(url) { |
43 |
| - var options = arguments[1] === undefined ? {} : arguments[1]; |
| 43 | + function fetchJsonp(_url) { |
| 44 | + var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; |
44 | 45 |
|
45 |
| - var timeout = options.timeout != null ? options.timeout : defaultOptions.timeout; |
46 |
| - var jsonpCallback = options.jsonpCallback != null ? options.jsonpCallback : defaultOptions.jsonpCallback; |
| 46 | + // to avoid param reassign |
| 47 | + var url = _url; |
| 48 | + var timeout = options.timeout || defaultOptions.timeout; |
| 49 | + var jsonpCallback = options.jsonpCallback || defaultOptions.jsonpCallback; |
47 | 50 |
|
48 | 51 | var timeoutId = undefined;
|
49 | 52 |
|
50 | 53 | return new Promise(function (resolve, reject) {
|
51 | 54 | var callbackFunction = options.jsonpCallbackFunction || generateCallbackFunction();
|
| 55 | + var scriptId = jsonpCallback + '_' + callbackFunction; |
52 | 56 |
|
53 | 57 | window[callbackFunction] = function (response) {
|
54 | 58 | resolve({
|
|
61 | 65 |
|
62 | 66 | if (timeoutId) clearTimeout(timeoutId);
|
63 | 67 |
|
64 |
| - removeScript(jsonpCallback + '_' + callbackFunction); |
| 68 | + removeScript(scriptId); |
65 | 69 |
|
66 | 70 | clearFunction(callbackFunction);
|
67 | 71 | };
|
|
70 | 74 | url += url.indexOf('?') === -1 ? '?' : '&';
|
71 | 75 |
|
72 | 76 | var jsonpScript = document.createElement('script');
|
73 |
| - jsonpScript.setAttribute('src', url + jsonpCallback + '=' + callbackFunction); |
74 |
| - jsonpScript.id = jsonpCallback + '_' + callbackFunction; |
| 77 | + jsonpScript.setAttribute('src', '' + url + jsonpCallback + '=' + callbackFunction); |
| 78 | + jsonpScript.id = scriptId; |
75 | 79 | document.getElementsByTagName('head')[0].appendChild(jsonpScript);
|
76 | 80 |
|
77 | 81 | timeoutId = setTimeout(function () {
|
78 | 82 | reject(new Error('JSONP request to ' + url + ' timed out'));
|
79 | 83 |
|
80 | 84 | clearFunction(callbackFunction);
|
81 |
| - removeScript(jsonpCallback + '_' + callbackFunction); |
| 85 | + removeScript(scriptId); |
82 | 86 | }, timeout);
|
83 | 87 | });
|
84 |
| - }; |
| 88 | + } |
85 | 89 |
|
86 | 90 | // export as global function
|
87 | 91 | /*
|
|
97 | 101 | throw new Error('polyfill failed because global object is unavailable in this environment');
|
98 | 102 | }
|
99 | 103 | }
|
100 |
| - |
101 | 104 | local.fetchJsonp = fetchJsonp;
|
102 | 105 | */
|
103 | 106 |
|
|
0 commit comments