title: How to chain fetch and promises in react native?
body: <p>Today I am struggling with a fetch issue in my react native app. I would like my user to login straight after he registers. In order to do that I have one function to fetch the signUp and another one to fetch the login. The problem is that when in the view I click on the register() button, only the signUp fetch is called. Though, if I click a second time on the button then both the signup and the login fetch are called but an error occurs on the signup because the user has already been added to the DB. So, I would like to be able to chain two fetch, one after another. Here is my code :</p>
  
  <p>The api helper class:</p>
  
  <pre><code>    const Api = {
  
    signUp(user) {
      return new Promise((resolve, reject) =&gt;
      {
        console.log(Constant.default.apiUrl);
        fetch(Constant.default.apiUrl + '/signup', {
          method: 'POST',
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
          },
          body: JSON.stringify(user)
        })
          .then(response =&gt; response.json())
          .then(responseJson =&gt; {
            if (responseJson.success) {
              console.log(responseJson.userData);
              resolve(responseJson.userData);
            } else {
              reject(responseJson);
            }
          })
          .catch(err =&gt; console.log(err));
      });
    },
  
    login(user) {
      return new Promise((resolve, reject) =&gt; {
        fetch(Constant.default.apiUrl + '/login', {
          method : 'POST',
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
          },
          body: JSON.stringify(user)
        })
          .then(response =&gt; response.json())
          .then(responseJson =&gt; {
            console.log("responsejson " + responseJson);
            console.log("laaaa");
            if (responseJson.success) {
              resolve(responseJson.userData);
            } else {
              reject(responseJson);
            }
          })
          .catch(err =&gt; console.log("catch login" + err))
      })
    }
  };
  
  export default Api;
  </code></pre>
  
  <p>The register function (when I click register button):</p>
  
  <pre><code>register () {
  
      if (this.state.password.length &gt; 3 &amp;&amp; this.state.password === this.state.confirmPassword) {
        var user = {
          firstName: this.props.firstName,
          lastName: this.props.lastName,
          emailAddress: this.state.emailAddress,
          password: this.state.password,
        };
  
        Api.signUp(user)
          .then(result =&gt; {
            console.log("sign up result : " + JSON.stringify(result));
            return {
              emailAddress : user.emailAddress,
              password : user.password
            };
             })
          .then(loginUser =&gt; {
            Api.login(loginUser)
              .then(result =&gt; {
                console.log("login result " + JSON.stringify(result));
              })
              .catch(resultErr =&gt; console.log(resultErr));
          })
          .catch(resultErr =&gt; console.log(resultErr));
  
      } else {
        this.setState({error: true,
          errorMessage: 'Veuillez remplir les champs correctement'});
      }
    };
  </code></pre>
  
  <p>Please help me figure out why I have to click twice on register() button.</p>
  
  <p>Thank you in advance for your answers :)</p>
  
  <p>EDIT: 
  When I click once, nothing happens in the console but the user is added to the DB. The second time, I see this : 
  <a href="https://i.stack.imgur.com/qbqaU.png" rel="nofollow"><img src="https://i.stack.imgur.com/qbqaU.png" alt="enter image description here"></a></p>