Hacker News new | ask | show | jobs
by simonw 1519 days ago
Question about https://edge-functions-examples.netlify.app/example/rewrite

    export default async (request: Request, context: Context) => {
      return context.rewrite("/something-to-serve-with-a-rewrite");
    };
I'm surprised that the function is async but context.rewrite() doesn't use an await. Is that because the rewrite is handed back off to another level of the Netlify stack to process?
3 comments

Promises are flat, so if a async function or promise callback returns a promise the result is just a promise, not promise<promise>.

Using async for functions that do not use await is still a good idea because thrown errors are converted to rejected promises.

`return await` can be useful because it's a signal that the value is async, causes the current function to be included in the async stack trace, and completes local try/catch/finally blocks when the promise resolves

Actually `context.rewrite` returns a `Promise<Response>`. The `async` isn't necessary here, but it also doesn't particularly hurt. You can return a `Promise` from an async function no problem.
Since it's being returned it doesn't really matter whether `.rewrite()` is returning a promise or not. `return await x` is mostly equivalent to `return x` within an async function.