Response promise resolves before endpoint finished processing
I want to interact with OpenAI's API in my Next.js + tRPC app. It seems that the my frontend is not waiting for the backend to finish the API call before using the response, leading to an error because the response is still undefined.
This is my endpoint:
I am calling from the frontend like this:
I am getting the error:
I have tried changing the endpoint to
useQuery
, which caused a different set of problems (calling the endpoint on each change to userInput
or never when using an intermediate variable changed only in handleSubmit
).1 Reply
Note: when logging the result of the OpenAI API call, the error appears before the result is logged correctly.