Skip to content

Commit 236e979

Browse files
committed
feat: useAsync
1 parent 36ce7c9 commit 236e979

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ const app = createApp()
3636
app.use('/api', (req) => ({ url: req.url }))
3737
app.use(() => 'Hello world!')
3838

39+
// app.useAsync(async () => {})
40+
// app.use('/big', () => import('./big'), { lazy: true })
41+
3942
const port = process.env.PORT || 3000
4043
const server = new Server(app.handle)
4144

@@ -65,6 +68,9 @@ Converts a classic middleware (`req, res, next?`) into promisifier version ready
6568
- If calling middleware throws an immediate error, promise will be rejected
6669
- On `close` and `close` events of res, promise will `resolve/reject` (to ensure if middleware simply calls `res.end`)
6770

71+
When calling `app.use`, middleware will be automatically promisified.
72+
If you are already making an async aware middleware, can use `app.useAsync`
73+
6874
## License
6975

7076
MIT

src/app.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ export function createApp (options: AppOptions = {}): App {
1717
app.stack = stack
1818
app.handle = handle
1919
// @ts-ignore
20-
app.use = (...args) => use(app, ...args)
20+
app.use = (arg1, arg2, arg3) => use(app, arg1, arg2, arg3)
21+
// @ts-ignore
22+
app.useAsync = (arg1, arg2) =>
23+
use(arg1, arg2 !== undefined ? arg2 : { promisify: false }, { promisify: false })
2124

2225
return app as App
2326
}

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export interface App {
3737
stack: Stack
3838
handle: PHandle
3939
use: AppUse
40+
useAsync: AppUse
4041
}
4142

4243
export interface AppOptions {

0 commit comments

Comments
 (0)