Skip to content
Advertisement

Google Cloud Function async with multiple fetch requests

I’m new to both GCF and Javascript async and have been struggling with this. I perform a fetch call initially and then pass that response as a parameter to a second function which then also performs a separate fetch call.

During the second function, my empty initialized json gets properties added to it, and when that function completes, I want to notify the exports.helloHttp to then do res.end and terminate.

I’ve tried chaining an additional empty then() but it doesn’t seem to be working.

My code:

JavaScript

Advertisement

Answer

Here is the code that would do what you want (replace the fetch URLs and set the appropriate options)

JavaScript

The same function can use an await like this

JavaScript

Finally you would also need to make sure that the package.json has the dependency for node-fetch

JavaScript

For sending the JSON response, it uses this method.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement