Skip to content

Middleware with aysnc? #192

Answered by kwhitley
osseonews asked this question in Q&A
Discussion options

You must be logged in to vote

Sounds like I need to address that in the docs (https://github.com/kwhitley/itty.dev/blob/main/src/routes/itty-router/middleware/+page.md)!

So true to the claim, these both work equally:

// sync middleware
const middleware1 = (request) => {
  request.foo = 'bar'
}

// async middleware
const middleware2 = async (request) => {
  request.foo = await db.getItems()
}

This is because the value of every middleware is simply awaited (because even sync functions can be awaited)... if it is undefined/void, the middleware is assumed to have not returned, and the flow continues to the next handler until that route is exhausted, at which point the router continues to match the next routes. If the value

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by kwhitley
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants