Skip to content

Commit ecfe37e

Browse files
committed
fix typo and build
1 parent 30af095 commit ecfe37e

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

build/fetch-jsonp.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
return 'jsonp_' + Date.now() + '_' + Math.ceil(Math.random() * 100000);
2424
}
2525

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
2728
function clearFunction(functionName) {
2829
// IE8 throws an exception when you try to delete a property on window
2930
// http://stackoverflow.com/a/1824228/751089
@@ -39,16 +40,19 @@
3940
document.getElementsByTagName('head')[0].removeChild(script);
4041
}
4142

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];
4445

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;
4750

4851
var timeoutId = undefined;
4952

5053
return new Promise(function (resolve, reject) {
5154
var callbackFunction = options.jsonpCallbackFunction || generateCallbackFunction();
55+
var scriptId = jsonpCallback + '_' + callbackFunction;
5256

5357
window[callbackFunction] = function (response) {
5458
resolve({
@@ -61,7 +65,7 @@
6165

6266
if (timeoutId) clearTimeout(timeoutId);
6367

64-
removeScript(jsonpCallback + '_' + callbackFunction);
68+
removeScript(scriptId);
6569

6670
clearFunction(callbackFunction);
6771
};
@@ -70,18 +74,18 @@
7074
url += url.indexOf('?') === -1 ? '?' : '&';
7175

7276
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;
7579
document.getElementsByTagName('head')[0].appendChild(jsonpScript);
7680

7781
timeoutId = setTimeout(function () {
7882
reject(new Error('JSONP request to ' + url + ' timed out'));
7983

8084
clearFunction(callbackFunction);
81-
removeScript(jsonpCallback + '_' + callbackFunction);
85+
removeScript(scriptId);
8286
}, timeout);
8387
});
84-
};
88+
}
8589

8690
// export as global function
8791
/*
@@ -97,7 +101,6 @@
97101
throw new Error('polyfill failed because global object is unavailable in this environment');
98102
}
99103
}
100-
101104
local.fetchJsonp = fetchJsonp;
102105
*/
103106

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"dependencies": {
1919
"es6-promise": "^2.3.0"
20-
},
20+
},
2121
"devDependencies": {
2222
"babel": "^5.8.21",
2323
"babel-core": "^5.8.21",

0 commit comments

Comments
 (0)