我是否以正确的方式使用.then()?

时间:2018-12-04 20:35:33

标签: javascript react-native state

就像标题中所说的,基于运行authHandler(请参阅下文),很明显它没有按照我想要的去做。但是我不知道为什么。

基本上,我要authHandle进行的工作是首先注册一个新用户,生成一个userUID和令牌,然后更新状态。然后,一旦实现,请运行代码的后半部分以存储新用户的用户名。

现在。它所做的只是注册用户(因此启动代码的前半部分),而不执行后半部分.then()。

我怀疑这与authHandler的上半部分和下半部分之间的状态有关。

我希望有人可以帮助我弄清楚我出了什么问题> << / p>

authHandler = () => {
    return new Promise ((resolve, reject) => {
        const authData = {
            email: this.state.controls.email.value,
            password: this.state.controls.password.value
        };
        this.props.onTryAuth(authData, this.state.authMode); // registers a new user and gives a userUID and token
    })
    .then(() => {
        this.props.onAddUserData(
          this.state.controls.userName.value,
       )
    }) //store the username of the new user
    .catch(err => {
        console.log(err);
        alert("Oops! Something went wrong, please try again")
    })
};

1 个答案:

答案 0 :(得分:0)

您需要resolvereject所创建的承诺-现在它处于待定状态,因此then的{​​{1}}中被调用的任何内容永远不会发生。您还应该返回诺言,在无法正常工作的地方呼叫authHandler()then。该代码段以一种可行的方式对其进行了重新组织。

catch