简体   繁体   中英

async function inside sync function dont return value

I'm building a function that is checking a user password with linux password shadow file. The problem is that the main (sync) function is returning undefined before the inner async function is finish, and because of that it always return undefined.

How can I change the main function to async so it will wait for the async function to finish?

Please advice. Thnaks in advance

Adrian

  function checkPass(a, b) {
    "use strict";
    var fs = require('fs');
    var sha512crypt = require('sha512crypt-node');
    //
    var shadow = '/tmp/shadow';
    var userNameCheck = a;
    var passwordCheck = b;
    //
    var response;

    fs.readFile(shadow, function (err, logData) {
        if (err) throw err;

        // logData is a Buffer, convert to string.
        var shadowFile = logData.toString();
        var shadowList = shadowFile.split('\n');

        for (var line in shadowList) {
            var userEntree = shadowList[line].split(":");

            if (userEntree[0] === userNameCheck) {

                var username = userEntree[0];
                var fullShadow = userEntree[1];
                //
                var fullShadowSplit = userEntree[1].split('$');
                //
                var passwordAlgorithm = fullShadowSplit[1];
                var saltShadow = fullShadowSplit[2];
                var passwordShadow = fullShadowSplit[3];
                //

                var hash = sha512crypt.sha512crypt(passwordCheck, saltShadow)
                //console.log(hash + '===' + fullShadow);
                if (hash === fullShadow) {
                    console.log('LOG: oldPassCorrect');
                    //return 'oldPassCorrect';
                    response = 'oldPassCorrect';
                    return response;
                    break;
                } else {
                    console.log('LOG: oldPassIncorrect');
                    //return 'oldPassCorrect';
                    response = 'oldPassIncorrect';
                    return response;
                    break;
                }
            }
        }
    });
}


console.log("Function response: " + checkPass('share', 'qwerty'));

I add a callback to the main function:

function checkPass(a, b, callback) {
    "use strict";
    var fs = require('fs');
    var sha512crypt = require('sha512crypt-node');
    //
    var shadow = '/tmp/shadow';
    var userNameCheck = a;
    var passwordCheck = b;
    //
    var response;

    fs.readFile(shadow, function (err, logData) {
        if (err) throw err;

        // logData is a Buffer, convert to string.
        var shadowFile = logData.toString();
        var shadowList = shadowFile.split('\n');

        for (var line in shadowList) {
            var userEntree = shadowList[line].split(":");

            if (userEntree[0] === userNameCheck) {

                var username = userEntree[0];
                var fullShadow = userEntree[1];
                //
                var fullShadowSplit = userEntree[1].split('$');
                //
                var passwordAlgorithm = fullShadowSplit[1];
                var saltShadow = fullShadowSplit[2];
                var passwordShadow = fullShadowSplit[3];
                //

                var hash = sha512crypt.sha512crypt(passwordCheck, saltShadow)
                //console.log(hash + '===' + fullShadow);
                if (hash === fullShadow) {
                    console.log('LOG: oldPassCorrect');
                    response = 'oldPassCorrect';
                    callback(response);
                    break;
                } else {
                    console.log('LOG: oldPassIncorrect');
                    response = 'oldPassIncorrect';
                    callback(response);
                    break;
                }
            }
        }
    });
}


checkPass('share', 'qwerty', function (response) {
        console.log("CallBack: " + response);
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM