我有一个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.')
}
答案 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.')
}
,因为如果您不运行请求,则合法地未定义它。