Using CO in node with node-mysql

0

I would like to know if someone can help me with the use of CO in node using node-mysql to build queries where one depends on the result of another. This is the same as using async / await but without needing Babel. I'm trying to do a function where I solve the promise with yield and this promise resolved to pass that value to another query and show the results in a later view.

    
asked by Walter Sosa 22.11.2016 в 20:09
source

1 answer

0

Taking into account the code in link you can take the wrap method and adapt it to your needs, like this:

function wrap (fn, ctx) {
  return function () {
    var args = [].slice.call(arguments);
    return function (done) {
      args.push(done);
      fn.apply(ctx, args);
    };
  };
}

// Asumiendo una conexión "conn" más arriba
conn.query = wrap(conn.query, conn);

Finally, to make your query, you do:

var co = require('co');
co(function *() {
  console.log(yield conn.query("SELECT 1+1 AS solution"));
  console.log(yield conn.query("SELECT 1+2 AS solution"));
  console.log(yield conn.query("SELECT 1+3 AS solution"));
});

P.D.: Always try to provide a preview of what you have done, so more people are interested in answering the question.

    
answered by 22.11.2016 в 23:49