nodeJS return value from callback

前端 未结 1 485
情歌与酒
情歌与酒 2021-01-02 17:45

I am building an application in which on a particular call, i must block and wait for the results from an authentication query before coninuing.

function aut         


        
相关标签:
1条回答
  • 2021-01-02 18:35

    Your outer function needs to provide a callback itself which can be executed once the mysql call is done. Something along the lines of this:

    function authenticate(user, pass, callback) {
        mysql_client.query("...", function (err, results, fields) {
            if (err) {
                callback("Error communicating ...");
            } else if (results.length ...) {
                callback("Error comparing authentication...");
            }
            callback()
        });
    });
    

    Example usage:

    authenticate('jim', '123456', function (err) {
        if (err) {
            alert(err);
        } else {
            alert('Welcome');
        }
    }); 
    
    0 讨论(0)
提交回复
热议问题