Socket.io: Compatible with async/await ?

Created on 9 Aug 2016  ·  3Comments  ·  Source: socketio/socket.io

I'm trying to integrate socket.io with koa v2.
Can I pass a async funtion as a event on server side,
just like:

socket.on('message', async () => {
  const some1 = await fun1()
  const some2 = await fun2()
  socket.emit('reply', some1, some2)
  ... 
})

As I known, the async funtion just a wrapper of promise.
Is there any implicit problems?

Most helpful comment

For anyone coming via Google, please be aware of this critical issue using await and socket.io:

https://github.com/socketio/socket.io/issues/3431

All 3 comments

That works just fine. In case fun1 or fun2 can throw, you might want to add try/catch

thx a lot : )

For anyone coming via Google, please be aware of this critical issue using await and socket.io:

https://github.com/socketio/socket.io/issues/3431

Was this page helpful?
0 / 5 - 0 ratings