Synchronous function returns no value unless await
Hello everyone,
I'm encountering a confusing issue while working with Hono.js. In my project, I have a function named getEbayAuthUrl which is not defined as an async function and simply returns a string, e.g., "hi". However, I've noticed that if I do not use await when calling this function, the value doesn't seem to be processed correctly, as if it were missing in subsequent database operations.
Here's a simplified version of the function:
It doesn’t work as expected, and it seems like the value isn't recognized in subsequent database operations.
VS.
I thought await was only necessary for functions returning Promises. Does anyone know why I would need to use await here even though the function doesn’t return a Promise? Is this something specific to Hono.js, or is there a general JS principle I’m missing?
5 Replies
Can you please provide the details of the
getEbayAuthUrl()
function (don't forget to omit any secrets/api keys if that's the case)
Also, have you tried to console.log("authUrl: ", authUrl
to actually see what's being returned vs what's being stored in the database?Thank you for answering. After debuging line by line, I realize the function starts with "use server"
I am not sure why using this would force to await the function even if it is not a async function.
you mean this is in a NextJS server action?
Yes thats correct. I follow DRY pratice to not repeat, and the function is reused from a server action.
Got it, that makes sense -- server actions are async by nature. (https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
Please provide all context next time so we can all better help you 🚀
Don't forget to mark this thread as Solved ✅