1 回答
TA贡献1842条经验 获得超12个赞
getInitialProps是一个async函数,这意味着您可以利用await语法。转换checkForCart成一个函数,返回一个promise和await它,然后处理结果。例如:
export const checkForCart = ({ cart, checkout }, res) => {
const { rows = [] } = checkout || {}
return new Promise((resolve, reject) => {
if (!cart || !rows.length) {
reject()
}
resolve()
})
}
Email.getInitialProps = async ({ req, res, apolloClient }) => {
const deviceInfo = getDeviceInfo(req)
try {
const {
data: { viewer },
} = await apolloClient.query({
query: GET_CHECKOUT,
fetchPolicy: 'network-only',
})
// If this rejects/fails because !cart || !rows.length
// execution will jump to the catch block
await checkForCart(viewer, res)
// This won't run until checkForCart finishes and resolves
checkForProcessingPayment(viewer, res)
return {
namespacesRequired: ['buy-common', 'common', 'buy-email'],
deviceInfo,
}
} catch (error) {
const { href, as } = getLinkProps('CART')
return redirect({ href, as }, res)
}
}
添加回答
举报