异步尝试/捕获中有多个等待。我如何使一个条件?

时间:2019-09-27 20:00:50

标签: javascript

我有一个try / catch函数,它具有多个返回响应的await函数:

const { data: { req1 } } = await this.props.submitMember(payload1)
const { data: { req2 } } = await this.props.submitSomething(payload2)
const { data: { req3 } } = await this.props.submitThing(payload3)

if (!req.success || !req2.success || !req3.success) {
  return addNotification('Something went wrong.')
}

我如何设置一个条件?例如:

const { data: { req1 } } = await this.props.submitMember(payload1)
const { data: { req2 } } = await this.props.submitMember(payload2) // IF SOME VARIABLE IS TRUE, DO NOT RUN THIS REQUEST
const { data: { req3 } } = await this.props.submitMember(payload3)

if (!req.success || !req2.success || !req3.success) {
  return addNotification('Something went wrong.')
}

1 个答案:

答案 0 :(得分:2)

只需在条件子句下放置一个:

9072019

访问9/07/2019的结果时,您必须测试const { data: { req1 } } = await this.props.submitMember(payload1); const { data: { req2 } } = mustCheck ? await this.props.submitMember(payload2) : { data: { req2: { success: true } } } // Did not run, did not fail. ; const { data: { req3 } } = await this.props.submitMember(payload3); if (!req.success || !req2.success || !req3.success) { return addNotification('Something went wrong.') } ,因为如果您不运行请求,则合法地未定义它。