.then链接等待,然后尝试捕获语法

时间:2019-10-08 01:01:44

标签: javascript firebase promise async-await

我有一个提交功能

const onSubmit = async (creditCardInput) => {
const { navigation } = this.props;
this.setState({ submitted: true });
let creditCardToken;

try {
  creditCardToken = await getCreditCardToken(creditCardInput);
  if (creditCardToken.error) {
    this.setState({ submitted: false});
    return;
  }
} catch (e) {
  this.setState({ submitted: false });
  return;
}


try {
  const obj = await subscribeUser(creditCardToken);
  console.log('returned obj', obj);
  try {
    const docRef = await this.db.collection("users").doc(this.user.email).set({
      id: this.user.uid,
      subscriptionId: obj.customerId,
      active: true
    });
    console.log("Document written with ID: ", docRef);
    this.navigateToScreen('HomeScreen');
  } catch (e) {
    console.log("Error adding document: ", e);
  }
} catch (error) {
  console.log('catch error', error);
  this.setState({ submitted: false, error: SERVER_ERROR, message: error });
}  
};

这正按怀疑的方式工作-当this.db.collection调用失败时,实现了catch,这意味着它注销了“错误添加文档:”,e 参见下面的代码段

  try {
    const docRef = await this.db.collection("users").doc(this.user.email).set({
      id: this.user.uid,
      subscriptionId: obj.customerId,
      active: true
    });
    console.log("Document written with ID: ", docRef);
    this.navigateToScreen('HomeScreen');
  } catch (e) {
    console.log("Error adding document: ", e);
  }

但是,当我以另一种方式实现订阅用户功能(见下文)时,我不再使用try catch,而是使用.then,然后等待内部函数失败并执行外部catch语句,这意味着它注销了console.log('catch error',error);什么时候应该注销console.log(“添加文档时出错:”,错误); 这是为什么?它们不应该以相同的方式工作,这意味着上方和下方的代码段应以相同的方式工作 参见下面的代码

await subscribeUser(creditCardToken).then((obj)=> {
this.db.collection("users").doc(this.user.email).set({
    id: this.user.uid,
    subscriptionId: obj.customerId,
    active: true
}).then((docRef) => {
    console.log("Document written with ID: ", docRef);
    this.navigateToScreen('HomeScreen');
}).catch((errors) => {
    console.log("Error adding document: ", errors);
});
}).catch((error) => {
    console.log('catch error', error);
    this.setState({ submitted: false message: error });
});

当this.db.collections成功解析.then时,只需添加一条注释即可,它应该以console.log(“ ID为ID的文档,” docRef)登出,但是就像我之前说过的,如果被拒绝外部捕获而不是内部捕获

还向this.db.collection函数添加return并删除await对结果没有影响

1 个答案:

答案 0 :(得分:0)

有两种处理方法-将set函数转换为使用异步/等待语法,或者将设置的函数返回转换为subscribeUser函数的下一个然后声明。我将同时显示两者,但我更喜欢第二个,因为我不喜欢将async / await与then / catch混合使用。

const tempThis = this
subscribeUser(creditCardToken).then((obj)=> {
  // RETURN this promise and you'll get it in the next then statement
  return tempThis.db.collection("users").doc(tempThis.user.email).set({
    id: tempThis.user.uid,
    subscriptionId: obj.customerId,
    active: true
  })
}).then((docRef) => {
  console.log("Document written with ID: ", docRef);
  this.navigateToScreen('HomeScreen');
}).catch((errors) => {
  // This will catch the errors from subscribeUser AND from the set function
  console.log("Error adding document: ", errors);
});

或仅使用异步/等待

try {
  const obj = await subscribeUser(creditCardToken)
  const docRef = await this.db.collection("users").doc(this.user.email).set({
    id: this.user.uid,
    subscriptionId: obj.customerId,
    active: true
  })
  console.log("Document written with ID: ", docRef);
  this.navigateToScreen('HomeScreen');
} catch (error) {
  // Handle your errors
}