JavaScript Javascript process function after double callback javascript process function

I have a problem with JavaScript callback functions. I'm using node Js and node-mysql to process mysql queries. When new users registered i need to do 2 db checks if This email already registered and if this nickname already exists and after this create INSERT statement. But the problem is that i don't know how to catch when email checked and nickname checked because they have 2 different callback functions. Please help me how to do this best way.

  1. Console.log looked like this!
  2. Checking nickname
  3. Checking Email!
  4. Inserting new row! -- THIS Must be last but it's here :(((
  5. Nickname CallBack!
  6. Email CallBack!

    checkNickName(nickname);  
    var checkMail = checkEmail(email);
    
    if(!checkMail){  // The problem is when this runs CallBack still not received from Mail and from NickName too
    console.log('Inserting new row!');
    connection.query("INSERT INTO `user` (`email`, `password`, `nickname`, `auth`, `token`, `balance`, `role`, `language`, `last_visit`, `register_date`) VALUES ('"+email+"', '"+password+"', '"+nickname+"', '"+auth+"', '"+token+"', '"+balance+"', '"+role+"', '"+language+"', CURRENT_TIMESTAMP, '"+registerDate+"');", function(err, rows, fields) {
        if (err) return 2;
    
        console.log(rows);
    
        return 3;
    });
    }
    
    
    
    
    function checkNickName(nickname){
        console.log('Checking nickname');
        connection.query("SELECT id FROM user WHERE `nickname` = '"+nickname+"'", function(err, rows, fields) {
            if (err) callback(err);
    
            callback(rows);
        });
    
        function callback(res){
            console.log('Nickname CallBack!');
            if (!res) { nickname = nickname + Math.floor((Math.random() * 10000) + 1); }
            return res;
        }
    }
    
    function checkEmail(email){
        console.log('Checking Email!');
        connection.query("SELECT id FROM user WHERE `email` = '"+email+"'", function(err, rows, fields) {
            if (err) callback('email',false);
    
    
            callback(rows);
        });
    
        function callback(res){
            console.log('Email CallBack!');
            if (!res) { nickname = nickname + Math.floor((Math.random() * 10000) + 1); }
            return res;
        }
    }
    
Answer:1

At the start of the function let us say you have a var counter = 0;

In check mail -> counter++;

In check nick name -> counter++;

now call a function in parallel to check mail, check nick name, let us name it as checkStatus

function checkStatus(){
    if(counter == 2) {
            // Call the callback
    } else {
        setTimeout(checkStatus, 1000);
    }

}
Answer:2

According to this link by mozilla, the data- attribute is used for setting data in the DOM. I want to grab the time using new Date().getTime() and append it to an element in the DOM. What is the ...

According to this link by mozilla, the data- attribute is used for setting data in the DOM. I want to grab the time using new Date().getTime() and append it to an element in the DOM. What is the ...

This site is probably overkill for the level of technical input I need for my query but the only other option is to ask Weebly support who are useless. Essentially I have a tracking pixel from google ...

This site is probably overkill for the level of technical input I need for my query but the only other option is to ask Weebly support who are useless. Essentially I have a tracking pixel from google ...

  1. adding tracking pixel to email
  2. adding tracking pixel to html
  3. adding a tracking pixel to website
  4. shopify adding a tracking pixel

I am working with dynamic web, this connected to websocket, then when my web load, i call a load.php (with angular directive). This web(load.php) makes a select in mySQL (i have many tables, linked ...

I am working with dynamic web, this connected to websocket, then when my web load, i call a load.php (with angular directive). This web(load.php) makes a select in mySQL (i have many tables, linked ...

This is my code: http://jsfiddle.net/Xk38X/6/ $('#register').click(function() { if( $('#company_f').val().length == 0 ) { $('#company_f').css("border", "solid 1px red"); } }); The ...

This is my code: http://jsfiddle.net/Xk38X/6/ $('#register').click(function() { if( $('#company_f').val().length == 0 ) { $('#company_f').css("border", "solid 1px red"); } }); The ...

  1. stop form submitting jquery
  2. stop form submitting on enter
  3. stop form submitting if validation fails
  4. stop form submitting using jquery
  5. stop form submitting javascript
  6. stop form submitting on page refresh
  7. stop form submitting
  8. stop form submitting twice
  9. stop form submitting on button click
  10. stop form submit on refresh php
  11. stop form submit js
  12. stop form submit jquery ajax
  13. stop form submit javascript validation
  14. stop form submit on enter javascript
  15. stop form submit on enter jquery
  16. stop form submit reloading page
  17. stop form submit on refresh
  18. stop form submit react
  19. stop form submit redirect
  20. stop form submit html