Skip to content

Commit 6cbef16

Browse files
committed
Enable Async HTTP requests
1 parent feda753 commit 6cbef16

File tree

1 file changed

+73
-35
lines changed

1 file changed

+73
-35
lines changed

ipgeolocation.js

Lines changed: 73 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -109,46 +109,84 @@ const _ipgeolocation = function() {
109109
urlParameters = addUrlParameter(urlParameters, "location", location);
110110
}
111111

112-
try {
113-
const response = await fetch("https://api.ipgeolocation.io/".concat(subUrl, urlParameters, ""), {
114-
method: "GET",
115-
headers: {
116-
"Accept": "application/json"
117-
}
118-
});
119-
const json = await response.json();
112+
if (window.fetch) {
113+
try {
114+
if (asyncCall) {
115+
fetch("https://api.ipgeolocation.io/".concat(subUrl, urlParameters, ""), {
116+
method: "GET",
117+
headers: {
118+
"Accept": "application/json"
119+
}
120+
})
121+
.then((response) => {
122+
if (!response.ok) {
123+
if (callback) {
124+
callback(JSON.parse("{'status': ".concat(response.status, ", 'message': '", json.message, "'}")));
125+
} else {
126+
console.error("status:", response.status, json);
127+
}
128+
}
120129

121-
if (!response.ok) {
122-
if (callback) {
123-
callback(JSON.parse("{'status': ".concat(response.status, ", 'message': '", json.message, "'}")));
130+
return response.json();
131+
})
132+
.then((json) => {
133+
if (useSessionStorage) {
134+
key = geolocationResponseName;
135+
136+
if (subUrl === timezoneEndpoint) {
137+
key = timezoneResponseName;
138+
} else if (subUrl === useragentEndpoint) {
139+
key = useragentResponseName;
140+
}
141+
142+
sessionStorage.setItem(key, JSON.stringify(json));
143+
}
144+
145+
if (callback) {
146+
callback(json);
147+
}
148+
});
124149
} else {
125-
console.error("status:", response.status, json);
126-
}
127-
}
128-
129-
if (useSessionStorage) {
130-
key = geolocationResponseName;
150+
const response = await fetch("https://api.ipgeolocation.io/".concat(subUrl, urlParameters, ""), {
151+
method: "GET",
152+
headers: {
153+
"Accept": "application/json"
154+
}
155+
});
156+
const json = await response.json();
131157

132-
if (subUrl === timezoneEndpoint) {
133-
key = timezoneResponseName;
134-
} else if (subUrl === useragentEndpoint) {
135-
key = useragentResponseName;
158+
if (!response.ok) {
159+
if (callback) {
160+
callback(JSON.parse("{'status': ".concat(response.status, ", 'message': '", json.message, "'}")));
161+
} else {
162+
console.error("status:", response.status, json);
163+
}
164+
}
165+
166+
if (useSessionStorage) {
167+
key = geolocationResponseName;
168+
169+
if (subUrl === timezoneEndpoint) {
170+
key = timezoneResponseName;
171+
} else if (subUrl === useragentEndpoint) {
172+
key = useragentResponseName;
173+
}
174+
175+
sessionStorage.setItem(key, JSON.stringify(json));
176+
}
177+
178+
if (callback) {
179+
callback(json);
180+
}
181+
}
182+
} catch (error) {
183+
console.error(error);
184+
185+
if (callback) {
186+
callback(JSON.parse("{'status': 400, 'message': 'Something went wrong while query ipgeolocation.io API. If the error persists, contact us at support@ipgeolocation.io'}"));
136187
}
137-
138-
sessionStorage.setItem(key, JSON.stringify(json));
139-
}
140-
141-
if (callback) {
142-
callback(json);
143-
}
144-
} catch (error) {
145-
console.error(error);
146-
147-
if (callback) {
148-
callback(JSON.parse("{'status': 400, 'message': 'Something went wrong while query ipgeolocation.io API. If the error persists, contact us at support@ipgeolocation.io'}"));
149188
}
150-
}
151-
189+
}
152190
}
153191

154192
function addUrlParameter(parameters, parameterName, parameterValue) {

0 commit comments

Comments
 (0)