I have a Promise setup to make an Http call like so (All code has been aggregated):
callHttpClient({ method, endpoint, payload }) {
return new Promise((resolve, reject) => {
axios({
method: method,
url: endpoint,
data: payload,
}).then(
(response) => {
if (response.status == httpStatusCodes.OK) {
if (response.data == undefined) {
reject(response);
} else {
logEventMixIn.methods.pushEvent('ApiResponse','Success', true);
resolve(response);
}
}
},
);
});
},
The true
flag on the pushEvent
will trigger another call inside of the logEventMixin.js
file which will also end up returning a Promise:
pushEvent(category, action, shouldLogToDatabase) {
var payload = {
userId: getUserId(),
sessionKey: getSessionKey(),
pageName: getPageName(),
sessionId: getSessionId(),
};
if(shouldLogToDatabase) {
httpHelperMixIn.methods.callEndpoint("https://pushEventEndpoint.com", payload);
// ** ^^ This returns a Promise ^^ **
}
....
},
The problem is when I pass my flag as true
to this method, I actually end up in an endless loop calling the same API endpoint thousands of times. I think it has something to do with the Promise chain, since if I place another Axios call directly in the logEventMixin.js
, I don't get the issue anymore (like so)
pushEvent(category, action, shouldLogToDatabase) {
var payload = {
userId: getUserId(),
sessionKey: getSessionKey(),
pageName: getPageName(),
sessionId: getSessionId(),
};
if(shouldLogToDatabase) {
axios({
method: "POST",
url: "https://pushEventEndpoint.com",
data: payload,
}).then((response) => {
Promise.resolve(response);
}
What am I doing wrong?