Api.fetch returns nothing (occasionally)

Hello guys,

my app needs data from different API’s. I’m using ‘@forge/api’ to make those API calls.
I’ve noticed that occasionally those fetches return nothing (not even undefined or null). When this happens and I check the API’s by hand the return data just fine, so it doesn’t seem to be a problem with those.
I’m a bit confused, because sometimes it just stopps working and I don’t see a reason for it to happen. The most mysterious thing is, that after a certain amount of time, the fetches return the data as expected. I’m leaving work and the programm doesn’t work, but when I return in the morning it works, without me having to change a single line of code.

For reference a example call:

const response = await api.fetch(url, {
    method: 'POST',
    headers: headers,
    body: req,
  });

console.log(response) // no log entry sometimes

Sorry for the long post, but has anyone experienced the same? Or has an idea why this might happen?

If it’s a long running request you might be running up against some of Forge’s runtime limits?

If you run with Forge tunnel you might see more details.

Ok is there a way to increase this limit or a way to compensate for it? I’d like to retry the API call if it doesn’t succeed but since I don’t get an error or an undefined/null variable I don’t know how.
Any help is greatly appreciated.

Not sure if this is the problem you’re seeing but just in case I’ll write down a detail that first tripped me up in Forge.

When your Forge functions (at least when used as trigger, likely the same for webtrigger and scheduled triggers) need to handle async calls and their responses then the function that is registered in the manifest needs to return a promise that resolves after all of your async calls have resolved. Otherwise the Forge runtime seems to kill it soon after that function returns.

I’m more familiar with plain promises rather than async/await so I’ll give an example using that instead.

If your Forge function would looks something like this (that function would be directly referenced in the manifest):

export const triggerFunction = () => {
	const responsePromise = api.fetch(url, {
		method: 'POST',
		headers: headers,
		body: req,
	});

	responsePromise.then((response) => {
		console.log(reponse);
	});
};

Then the console.log response might not be executed because the function return value didn’t indicate that there is a pending promise that needs to be waited for. The fix would be just returning the promise that is still pending:

export const triggerFunction = () => {
	// ...
	return responsePromise.then((response) => {
		console.log(reponse);
	});
};

As I said, not sure if this is your problem or how this behaves exactly when using await instead.

1 Like

I don’t want to jump to an early conclusion…but for now it works perfectly fine!
I’ll keep on testing and report back asap.
Thank you!