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
Post a Comment