H
Hono5mo ago
Jing

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:
function getEbayAuthUrl() {
return "MyAuthUrl";
}

const app = new Hono();

app.post(
'/',
verifyAuth(),
zValidator(
'json',
insertOauthCredentialSchema.pick({
clientId: true,
clientSecret: true
}),
),
async (c) => {
const values = c.req.valid('json');

const authUrl = await getEbayAuthUrl(values);
const inserted = await db
.insert('oauthCredentials')
.values({
authorizationUrl: authUrl,
...values,
})
.returning('*');

return c.json({ data: inserted });
},
);

export default app;
function getEbayAuthUrl() {
return "MyAuthUrl";
}

const app = new Hono();

app.post(
'/',
verifyAuth(),
zValidator(
'json',
insertOauthCredentialSchema.pick({
clientId: true,
clientSecret: true
}),
),
async (c) => {
const values = c.req.valid('json');

const authUrl = await getEbayAuthUrl(values);
const inserted = await db
.insert('oauthCredentials')
.values({
authorizationUrl: authUrl,
...values,
})
.returning('*');

return c.json({ data: inserted });
},
);

export default app;
It doesn’t work as expected, and it seems like the value isn't recognized in subsequent database operations.
const authUrl = getEbayAuthUrl(values);

# Shows {} in database
const authUrl = getEbayAuthUrl(values);

# Shows {} in database
VS.
const authUrl = await getEbayAuthUrl(values);

# Shows "MyAuthUrl" in database
const authUrl = await getEbayAuthUrl(values);

# Shows "MyAuthUrl" in database
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
Joaquim Ley
Joaquim Ley5mo ago
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?
Jing
JingOP5mo ago
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.
Joaquim Ley
Joaquim Ley5mo ago
you mean this is in a NextJS server action?
Jing
JingOP5mo ago
Yes thats correct. I follow DRY pratice to not repeat, and the function is reused from a server action.
Joaquim Ley
Joaquim Ley5mo ago
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 ✅
Want results from more Discord servers?
Add your server