Avoiding callback hell using generators and promis

2019-03-06 04:16发布

问题:

I am new to nodejs. Here, I want to avoid callback using Co generators with promises. But when I execute this code, it only executes first yield or does not assign the result of get function to processedData0 variable. How to solve this ?

co(function *() {    

  console.log("hello");

  try{
    console.log("hello");
    var processedData0= yield Promise.promisify(get);
    //console.log("hello");
    var processedData1 = yield Promise.promisify(abc1)(processedData0);
    //console.log("hello2");
    var processedData2 = yield Promise.promisify(xyz)(processedData1);
    //console.log("hello3");
    var processedData3 = yield Promise.promisify(pqr)(processedData2);
    // console.log("success");

  } catch (err) {
    throw err;
  }
})

This is my get function :-

 function get(err,rows){

connection.connect(function(err){


                                     if (err) 
                                            {
                                                    console.error('error connecting: ' + err.stack);
                                                    return;
                                                 }
                                                 else{
                                                     console.log("connected to database");
                                connection.query("SELECT count(id) as count from abc where name=? AND email=?",[username,password],function(err,rows){
                                    if(err){throw err}
                                    else{

                                                 b=rows;
                                                return rows;
                                                 console.log(b);
                                    }
                                });



                                                    }

                   });

                     }

This is my abc1 function:-

function abc1(err,rows){

              console.log("onresult");
              if(err)
                     {

                   throw err;
                   console.log("error in Query");
                     }
              else
                     {
                   if(rows[0].count!=1)
                                         {
                                                   //xyz(result,xyz);
                                       res.json({

                                                "status": 401,
                                                "message": "Invalid credentials"
                                               });
                                         }
                   else
                        {         connection.query("SELECT email from abc where name=? AND email=?",[username,password],function(err,rows){} );
                                  //res.json(genToken("ayush"));
                        }
                     }
                        }

This is my xyz function:

function xyz(err,rows){
                    if(err)
                             {
                               console.log(err);
                             }
                    console.log(rows);
                    connection.query("SELECT name from abc where name=? AND email=?",[username,password],function(err,rows){} );
                 }

This is my pqr function:

function pqr(err,rows){
                   if(err)
                             {
                               console.log(err);
                             }
                    console.log(rows);
                         }