Delay in returning value from backend module in time?

I have a backend module function that handles registering a user after it is sent from my lightbox. Depending on the response I get back from this function will depend on if an error message is given to a user or they get redirected elsewhere if it is successful.

If the signup is successful I want to return the string “SUCCESS”. However when I call the wix-users-backend register from within the function there seems to be some form of delay as my client-side codes gets an undefined result coming back meaning my page automatically throws an error, so the “SUCCESS” is never actually passed back, so even though a user will complete the signup an error will throw on the signup page as if it wasn’t successful.

Here is my server-side code:

import wixData from 'wix-data';
import wixUsersBackend from 'wix-users-backend';
import {searchForEmail} from 'backend/checkAccounts';

export function registerUser(_email, _pass) {
 let regExpEmail = new RegExp("^([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{0,50})$");
 let regExpPass = new RegExp("^([A-Za-z_][A-Za-z0-9_]*){6,30}$");

 if (regExpEmail.test(_email) && regExpPass.test(_pass)) {
 //Check UserInfo database to see if email has already been setup there, if so can create user login with wixUsers.register()
 return wixData.query("UserInfo")
        .eq("email", _email)
        .find({suppressAuth: true})
        .then( (results) => {
 if (results.items.length > 0) {
                searchForEmail(_email) //checks actual wix login database not UserInfo
                .then(result2 => {
 if (result2 === "FOUND") {
 //If this is ran then account already exists.
 return "EXISTS";
                    }else {
 //If here then email already exists in UserInfo but ISN'T in our wix login data. Therefore can register user.
                         wixUsersBackend.register(_email, _pass)
                         .then( () => {
                             console.log("REG STATUS", "Wix-users signup successful.");
 return "SUCCESS";
                         })
                         .catch( (err) => {
                             console.log("REG STATUS", "Wix-users signup error.");
 return "ERROR";
                         });
                    }
                });
            }else {
 return "NORESULTS";
            }
        });
    }
}

Here is the snippet of client code from the page I am having a problem with:

registerUser(email, $w("#password").value)
.then( (registerResults) => {
          console.log("RESULT FROM REGUSER CLIENTSIDE", registerResults);
          if (registerResults === "SUCCESS") {
             //Load thank you message
             wixWindow.openLightbox("ThanksSignup");
             wixWindow.lightbox.close("Signup");
          }else if (registerResults === "EXISTS") {
              $w("#errorText").text = "You have already made an account. Try logging in or resetting your password!";
          }else {
             $w("#errorText").text = "There was an error making your account.";
         }
});

I have checked my site event logs and the console log from the client shows that the result from my user registration is undefined. Then a fraction of a second later I get my console log from the server stating the signup was successful:

How is this even possible and what am I missing? Thanks.

It seems you forgot some "return"s.
for example: return searchForEmail ( _email ) or:
return wixUsersBackend.register(_email, _pass)

Brilliant that seems to have fixed it!