0

I'm very confused with a code, before a form submit, it must request a token, but only submit if the token is caught.

my html submit button:

 <button type="button" class="buttons-profile" id="registernow" onclick="formValidate()">

my function formValitade

    <script>
    function formValidate(){    
      let res = requestPermission();
      if (res == true) {
         console.log('ok submit')
      } else {
         console.log('no token avaliable')
      }
    }
    </script>

And the function requestPermission()

<script>
function requestPermition(){
    let success = true;
    const messaging = firebase.messaging(); // it's ok
    messaging.getToken().then((currentToken) => {
        if (currentToken) {
            dtoken = currentToken; //dtoken is a variable in global scope           
        } else {
            console.log('No Instance ID token available. Request permission to generate one.');
        }       
    }).catch((err,deres) => {
        success = false;
        console.log('An error occurred while retrieving token. ', err);    
        
    });
        
    return success;
 
}
<script>

When formValidate() execute requestPermission(), the variable res have value "undefined", i think for a moment, because the formValidate is not assync, how i solve this?

0 Answers0