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.

4 Likes

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!

It’s not working for me either… Tried with both await and promise approach but nothing. Actually the results is returned:

{“headers”:{},“ok”:true,“status”:200,“statusText”:"
OK"}

But the data (response.json()) is never returned…

It actually works for me it just doesn’t print the result for some reason (using forge logs).

Hi Vladimir,

it may be that the size of the data to return is considered large, and the response is chunked.

Do you have the same problem using node-fetch in a standalone script? Maybe you need to use await in the get of the response data until all chunked data is fetched
let your data = await response.json();

L

Sorry for the late reply - hopefully you solved your issue by now.

For future searchers: Forge logs will clip results at a certain number of lines per lambda invocation but you will usually see a message to that effect. If you find the response.json() never seems to return it could be that the lambda crashed due to memory limits being exceeded or that it timed out if it ran longer than 25 seconds. I have encountered both of these issues in the past and it just fails silently.

Many thanks Jeff. Yes, it is working now.