how to wait for a finished method to execute the next in angular?

Asked

Viewed 1,959 times

0

Well, I’m doing an Angular course, I’m trying to understand the concept of Observables and Promisses, but I’m a little confused, I’m using Firebase to create a simple application, where I use email authentication and password and to save the other user data I have to use the id of this authenticated user and register in the Database. Having done so I have to use the method of my service that creates this user and then use the method that saves the data. Following this order I have to do the method of SignUp(email,senha) be executed before the method setUserData(User).

> Metodos

SetUserData(user) {
    const itemsRef = this.db.object(`usu/${user.uid}`);
    return itemsRef.set(user);
  }



SignUp(email, password) {
    return this.afAuth.auth.createUserWithEmailAndPassword(email, password)
      .then((result) => {
        this.SendVerificationMail();
        window.alert("BEM VINDO");
      }).catch((error) => {
        window.alert(error.message)
      })
  }

Note: The methods are called in the component where I do the two-way data Binding with the form using an onsubmit method'

2 answers

0


JS is asynchronous, you can work with callback for this, the first method takes the second method as argument, after the processing of the first, it terminates by calling the second:

function segundo(login) {
  console.log('segundo');
}

function primeiro(callback) {
  console.log('primeiro');
  callback();
}

primeiro(segundo);

Also scrolls do this with observable, with Promise:

  • Thank you very much, man, it worked here, I gave a good and good, it worked perfectly

0

  • Thank you very much helped, haha valeu bro !!

  • Not at all, friend!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.