diff --git a/channels/1ds-post-js/src/HttpManager.ts b/channels/1ds-post-js/src/HttpManager.ts index e8a112482..d09466f46 100644 --- a/channels/1ds-post-js/src/HttpManager.ts +++ b/channels/1ds-post-js/src/HttpManager.ts @@ -470,6 +470,23 @@ export class HttpManager { requestInit.headers = payload.headers; } + const handleResponse = (status: number, headerMap: { [x: string]: string; }, responseText: string) => { + if (!responseHandled) { + responseHandled = true; + _doOnComplete(oncomplete, status, headerMap, responseText); + _handleCollectorResponse(responseText); + } + }; + + const handleError = () => { + // In case there is an error in the request. Set the status to 0 + // so that the events can be retried later. + if (!responseHandled) { + responseHandled = true; + _doOnComplete(oncomplete, 0, {}); + } + }; + fetch(theUrl, requestInit).then((response) => { let headerMap = {}; let responseText = STR_EMPTY; @@ -482,22 +499,12 @@ export class HttpManager { if (response.body) { response.text().then(function(text) { responseText = text; - }); - } - - if (!responseHandled) { - responseHandled = true; - _doOnComplete(oncomplete, response.status, headerMap, responseText); - _handleCollectorResponse(responseText); - } - }).catch((error) => { - // In case there is an error in the request. Set the status to 0 - // so that the events can be retried later. - if (!responseHandled) { - responseHandled = true; - _doOnComplete(oncomplete, 0, {}); + handleResponse(response.status, headerMap, responseText); + }, handleError); + } else { + handleResponse(response.status, headerMap, ""); } - }); + }).catch(handleError); if (ignoreResponse && !responseHandled) { // Assume success during unload processing