gpt4 book ai didi

javascript - 异步 try/catch 中的多个等待。如何设置一个条件?

转载 作者:行者123 更新时间:2023-12-01 00:35:55 25 4
gpt4 key购买 nike

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

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.')
}

最佳答案

只需将一个放在条件子句下即可:

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.')
}

在访问 req2 的结果时,您必须测试 mustCheck,因为如果您没有运行该请求,它可能会合法地未定义。

关于javascript - 异步 try/catch 中的多个等待。如何设置一个条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58140730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com