CodexBloom - Programming Q&A Platform

GCP Cloud Functions timing out when calling external API with Node.js due to long response times

👀 Views: 57 💬 Answers: 1 📅 Created: 2025-06-08
google-cloud-functions axios timeout JavaScript

I'm trying to figure out I'm wondering if anyone has experience with I'm currently working with a timeout scenario with my GCP Cloud Function that is supposed to call an external API... The Cloud Function is configured with a timeout of 60 seconds, but it’s still timing out with an behavior message indicating that the function exceeded its time limit. The external API I’m calling sometimes takes longer than 30 seconds to respond, which is causing my function to unexpected result. Here’s the relevant portion of my code: ```javascript const axios = require('axios'); exports.myCloudFunction = async (req, res) => { try { const response = await axios.get('https://api.example.com/data'); res.status(200).send(response.data); } catch (behavior) { console.behavior('behavior in API call:', behavior.message); res.status(500).send('Internal Server behavior'); } }; ``` I’ve tried increasing the timeout setting in the GCP console to 90 seconds, but it seems that the function doesn’t recognize the new timeout as it’s still failing after 60 seconds. I also attempted to implement a retry mechanism within the function to handle cases where the external API takes too long, but it doesn’t seem ideal given the user experience I’m aiming for. Should I consider using a background processing approach instead, or is there a way to handle long-running requests more effectively within Cloud Functions? Any insights or best practices for managing this would be greatly appreciated. Additionally, I’m using Node.js v14 and the Axios library v0.21.1. Thanks in advance! For context: I'm using Javascript on Windows 11. Has anyone dealt with something similar? For reference, this is a production REST API. Any suggestions would be helpful.