node.js - Socket.io + co: Is this how it's supposed to be used? -


i trying http://socket.io/ working co.

i trying tasks asynchronously in code.

io.on('connection', function (socket) {      // <--- need heavy here      socket.on('something', function (data) {          // <--- need heavy here      });      // <--- need heavy here  }); 

that's how socket.io works. add co mix now. i've tried following:

io.on('connection', function (socket) {     co(function* () {          yield something(); // <--- works          socket.on('something', function (data) {              yield something(); // <--- not work          });          yield something(); // <--- works     }); }); 

get error: syntaxerror: unexpected strict mode reserved word

and this:

io.on('connection', function (socket) {     co(function* () {          yield something(); // <--- works          socket.on('something', function (data) {              co(function* () {                 yield something(); // <--- works             });          });          yield something(); // <--- works     }); }); 

my question is, how supposed used, or ther i've missed. seems awful lot of code wrap everything?

so thought again.

io.on('connection', co.wrap(function *(socket) {      yield something();      socket.on('something', co.wrap(function *(data) {          yield something();      }));      yield something(); })); 

this should trick. wrap returns function, return promise then. here don't care latter. co documentation on co.wrap


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -