The getInfoFrom3rdParty() call is an asynchronous event. However, you did not return a promise from your function, so the functions do not wait for the completion of the asynchronous event.
It seems to me that since you are returning undefined, the function also assumes that it failed and retries. At some point in the repetition process, the async event probably ends before the function exits (i.e., Unintended success due to race conditions). I have seen similar results in other cases where users do not return a promise or value in their functions.
I canโt tell from the essence of what you are trying to do - it does not seem to do anything with third-party results and is probably not a realistic mcve of your use case. But something like this, probably you want:
exports.getInfo = functions.https.onRequest((request, response) => { // .... // NOTE THE RETURN; MOST IMPORTANT PART OF THIS SAMPLE return getInfoFromThirdParty(...).then(() => { response.writeHead(200, {"Content-Type": "application/json"}); response.end(JSON.stringify(payload)); }).catch(e => /* write error to response */); }); function getInfoFrom3rdParty(food) { reqObj.body = '{"query": "'+food+'"}'; return new Promise((resolve, reject) => { mainRequest(reqObj, function (error, response, body) { // .... if( error ) reject(error); else resolve(...); // .... }); }); }
source share