最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to make Nodejs call synchronous then I can use it easily? - Stack Overflow

programmeradmin1浏览0评论
app.post('/login', function (req, res) {

    var login = req.body.login;
    var pass = req.body.pass;

    var ret = CheckUserValid(login, pass);

  res.send(ret);
})

function CheckUserValid(login, pass,callback) {
    var sql = "SELECT * FROM `user` WHERE login = ? AND pass= ?";
    client.query(sql, [login, pass], function selectResutl(err, results, fields) {
         console.log(results);
        if (!err) return true;
        else
            throw err;
    });
}

first Function is about request and second is about making call to mysql. because it's async so it's not worked. Can someone let me know how I can make it work synchronously like in C#.

app.post('/login', function (req, res) {

    var login = req.body.login;
    var pass = req.body.pass;

    var ret = CheckUserValid(login, pass);

  res.send(ret);
})

function CheckUserValid(login, pass,callback) {
    var sql = "SELECT * FROM `user` WHERE login = ? AND pass= ?";
    client.query(sql, [login, pass], function selectResutl(err, results, fields) {
         console.log(results);
        if (!err) return true;
        else
            throw err;
    });
}

first Function is about request and second is about making call to mysql. because it's async so it's not worked. Can someone let me know how I can make it work synchronously like in C#.

Share Improve this question asked May 31, 2012 at 20:19 ankitankit 973 silver badges12 bronze badges 1
  • 5 If you're going to use NodeJS, you might as well learn to write asynchronous code. The CheckUserValid function accepts a callback, so why not use it? – user1106925 Commented May 31, 2012 at 20:28
Add a ment  | 

3 Answers 3

Reset to default 4

The signature of your CheckUserValid method already implies a callback. You can use that to call something when your db-request is done, like this:

app.post('/login', function (req, res) {
  var login = req.body.login;
  var pass  = req.body.pass;

  // use a closure here for the callback of checkUserValid
  checkUserValid(login, pass, function(err, isValid) {
    // send the response when checkUserValid is done
    res.send(isValid)
  });
})

function checkUserValid(login, pass, callback) {
  var sql = "SELECT * FROM `user` WHERE login = ? AND pass= ?";
  client.query(sql, [login, pass], function(err, results, fields) {
    console.log(results);
    if (!err) {
      // there is no error, pass null for the error
      // and true for the result
      callback(null, true);
    } else {
      // an error occurred in the db-query
      // pass it to the callback
      callback(err)
    }
  });
}

By convention you should pass the err as first argument, but in the end that's up to you. However you can pass the original error up to your initial function and handle it there.

You should a look at Fibers module.

You can chain the functions that you want to call in order

e.g. you can make res.send(ret); as the callback of your CheckUserValid

发布评论

评论列表(0)

  1. 暂无评论