0

I would like to create a simple function in Javascript that returns me the result of calling Firebase.createUser() dependent on onComplete.

I've done like this:

function createAUser(email, password) {

if (email === "" ||  password === "") { return false; }

var
    onComplete = function (error, userData) {
        if (error) {
            switch (error.code) {
            case "EMAIL_TAKEN":
                console.log("The new user account cannot be created because the email is already in use.");
                break;
            case "INVALID_EMAIL":
                console.log("The specified email is not a valid email.");
                break;
            default:
                console.log("Error creating user:", error);
            }
            return false;
        } else {
            console.log("Successfully created user account with uid:", userData.uid);
            return true;
        }
    };

MyFirebaseRootRef.createUser({ email: email, password: password}, onComplete);
/* return something here, true/false based on onComplete */
}

Or.. are there any other way to get me what I want. What I'm essentially after is just to find a way to figure out not only through console.log() how the creating of a user went.

Sorry for typos/bad code, thanks for all responses!

Edit: Thanks for all responses I've now looked into the callback & the asynchronous stuff (something like starting another thread, and then follow through with the function). I must give it some thought over data, like the stack data in the function must be release upon return, how can this data be followed through to the callback.

Anyhow sorry for duplicate, thanks again

2 Answers2

2

You're dealing with asynchronous data.

The call to create the Firebase user goes over the network, which means we have to patiently wait for the user to come back. But, we don't want to block the only thread we have to do other operations. This means the call will go on the JavaScript event loop and when the network request completes (and the call stack is clear), we will finally get our data back.

To handle this elegantly in code you can use a Promise.

var user = { email: 'my@email.com', password: 'secret' };
var promise = new Promise() {

  function(resolve, reject) {

    var ref = new Firebase('<my-firebase-app>');
    ref.createUser(user, function(error, authData) {
      if (error) {
        reject(error);
      } else {
        resolve(authData);
      }
    });

  }

};

promise
 .then(function(authData) {

 })
 .catch(function(error) {

 });

Promises are native to modern browsers, but if you want to support older browsers you'll have to use a library like jQuery, Bluebird, or RSVP.

David East
  • 27,440
  • 6
  • 60
  • 78
1

onComplete is a callback. This means that your code is sending a request and when it is completed, onComplete is being called. When you call createUser, the request is being sent and is not completed, so at the line after calling createUser you are not able to get the result of onComplete, because the request is not finished yet. As a result, you need to handle the complete event inside the function you associated to it.

Lajos Arpad
  • 45,912
  • 26
  • 82
  • 148