HonoH
Hono2y ago
5 replies
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;


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

VS.
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?
Was this page helpful?