function not worked synchronously in nodejs?

I am little bit confused with my code it's not worked synchronusly as it's should be. I use everyauth to do authentication.

registerUser(function(newUserAttrs) {
        var login = newUserAttrs[this.loginKey()];
        user.CreateNewUser(newUserAttrs.login, newUserAttrs.password, newUserAttrs.email, function(res, err) {
            if(!err) {
                return usersByLogin[login] = newUserAttrs;
            }
            else {
                throw err;
            }

        });

    })

in another file I have write this code

exports.CreateNewUser = function(login, pass, mail, callback) {

    var sql = "insert into `user`(login,mail,pass) values(?,?,?)";
    client.query(sql, [login, mail, pass], function(err, results, fields) {
        if(!err) {
            callback(results);
            console.log('test')
        }
        else {
            callback(results, err);
        }
    });
};

This code are working fine. I have tested him. the only problem is they are working synchronosly (as normal). Can someone explain me what thing I have done in wrong way that make it async. I want to get it done in sync way.

The current code give me error (it's make a entry in database and produce error on browser)

Error: Step registerUser of `password` is promising: userOrErrors ; however, the step returns nothing. Fix the step by returning the expected values OR by returning a Promise that promises said values.
    at Object.exec (E:\proj\Node\node_modules\everyauth\lib\step.js:68:11)
    at E:\proj\Node\node_modules\everyauth\lib\stepSequence.js:26:38
    at [object Object].callback (E:\proj\Node\node_modules\everyauth\lib\promise.js:13:12)
    at RouteTriggeredSequence._bind (E:\proj\Node\node_modules\everyauth\lib\stepSequence.js:25:20)
    at RouteTriggeredSequence.start (E:\proj\Node\node_modules\everyauth\lib\stepSequence.js:52:33)
    at RouteTriggeredSequence.routeHandler (E:\proj\Node\node_modules\everyauth\lib\routeTriggeredSequence.js:13:13)
    at Object.<anonymous> (native)
    at nextMiddleware (E:\proj\Node\node_modules\connect\lib\middleware\router.js:175:25)
    at param (E:\proj\Node\node_modules\connect\lib\middleware\router.js:183:16)
    at pass (E:\proj\Node\node_modules\connect\lib\middleware\router.js:191:10)

Thanks

Because you are doing a database query, this code has to be asynchronous. The anonymous function you pass to client.query will not be called until the database query is complete, so your callback gets called asynchronously.

You will need to treat this all as asynchronous, so for instance you'll have to trigger some other callback instead of returning the user object/throwing.

The two pieces of code you present are asynchronous and not synchronous! With everyauth, to be able to handle asynchronous user creation you should use a Promise. So your code will be something like :

registerUser(function(newUserAttrs) {
    var promise = this.Promise();
    var login = newUserAttrs[this.loginKey()];
    user.CreateNewUser(newUserAttrs.login, newUserAttrs.password, newUserAttrs.email, function(res, err) {
        if(!err) {
            return promise.fulfill(newUserAttrs);
        }
        else {
            promise.fulfill(user);
        }

    });

})

Without promise you couldn't be sure that your new user has been added in your database. But if it doesn't matter you could have something like that:

registerUser(function(newUserAttrs) {
    var login = newUserAttrs[this.loginKey()];
    user.CreateNewUser(newUserAttrs.login, newUserAttrs.password, newUserAttrs.email, function(res, err) {
        if (err) console.log(err);
    });
    return newUserAttrs;

})