H
Hono4w ago
moroshko

How to perform an async operation in Web Socket's onMessage?

I'm using Web Sockets with Bun, and wondering what's the idiomatic Hono-way of awaiting a Promise in onMessage (which is a sync method)?
const { upgradeWebSocket, websocket } = createBunWebSocket<ServerWebSocket>();

new Hono().get(
"/",
upgradeWebSocket((c) => {
return {
onMessage(event, ws) { // not async :(
// How can await here something, e.g. await Bun.sleep(10);
},
...
}
})
)
const { upgradeWebSocket, websocket } = createBunWebSocket<ServerWebSocket>();

new Hono().get(
"/",
upgradeWebSocket((c) => {
return {
onMessage(event, ws) { // not async :(
// How can await here something, e.g. await Bun.sleep(10);
},
...
}
})
)
I tried to use setTimeout and Promise.then(), but their callbacks are never executed.
1 Reply
CasperX
CasperX4w ago
i think you could do something like this
onMessage(event, ws) {
// do async stuff here
const innner = async () => {
// do something
await Bun.sleep(10);
// do something else
}
inner()
}
onMessage(event, ws) {
// do async stuff here
const innner = async () => {
// do something
await Bun.sleep(10);
// do something else
}
inner()
}
Want results from more Discord servers?
Add your server