Reputation: 485
Hi I am working on an app using cloud functions. So the thing is that I am using the Blaze subscription and I am making multiple API calls to AWS / Azure servers. Those calls most of the time wont get resolved and the execution would just stop. By stop I dont mean timedout or something, by stop I mean literally stop indefinitely, I wont get any error or timeouts nothing. It would just stop working and responding
The thing is that it works fine on the local emulator, but as soon as I test it out on the server it would do that behavior.
May be I am missing something when trying to make async calls to functions or something?
Any help please?
This is the primary logic of the code: index.js
APIEngine.initialize(payload)
.then(async result => {
new ResourceEngine(result, payload)
.getResourceFromRegions()
.then(resources => {
console.log("signature resources: ", resources);
// new ScanEngine(
// resources.apiEngine,
// resources.response,
// payload.report
// ).run();
})
.catch(err => {
console.log("error ===>", err);
});
})
.catch(err => {
return console.log("ERROR ====>", err);
});
Upvotes: 0
Views: 358
Reputation: 124
If you are making an Async HTTP Request inside a Cloud Function and you are receiving a function terminated log in Stackdriver Logging before the execution of your async block, that means that you are not managing well the lifecycle of your function and you'll need to make a change in your code in order to avoid the problem of having calls not resolved.
You need to notify the function when the async call had finished.
If your function is an HTTP function (https://cloud.google.com/functions/docs/writing/http) you just need to response a 200 OK when you recieve the response of your promise => res.status(200).send("OK") inside the .then block.
If your function is a background function (https://cloud.google.com/functions/docs/writing/background) you need to return the promise to the function using "return"
Example:
return APIEngine.initialize(payload)
.then(async result => {
new ResourceEngine(result, payload)
.getResourceFromRegions()
.then(resources => {
console.log("signature resources: ", resources);
// new ScanEngine(
// resources.apiEngine,
// resources.response,
// payload.report
// ).run();
})
.catch(err => {
console.log("error ===>", err);
});
})
.catch(err => {
return console.log("ERROR ====>", err);
});
Upvotes: 1