我有react-native / graphql / apollo应用程序。用户发出请求以从服务器获取凭据,并通过异步/等待将其写入钥匙串。如果成功,则用户必须从“身份验证”导航到主屏幕。我在钥匙串异步/等待后使用.then进行导航。有人可以告诉我这是一个好方法还是我不需要使用.then?
const AuthScreen = ({ navigation }) => {
const sign = useMutation(SIGN_IN)
const handleSignIn = code => {
sign({
variables: { code },
update: async (cache, { data }) => {
const accessToken = data.signIn.accessToken
const refreshToken = data.signIn.refreshToken
await Keychain.setGenericPassword(accessToken, refreshToken)
}
}).then(() => navigation.navigate('Home'))
}
const getToken = async () => {
// setLoading(true)
RNAccountKit.configure({
responseType: 'code',
initialPhoneCountryPrefix: '+7',
initialPhoneNumber: '9855316514',
defaultCountry: 'RU'
})
const payload = await RNAccountKit.loginWithPhone()
console.log('payload.code', payload.code)
handleSignIn(payload.code)
}
const { container } = styles
return (
<View style={container}>
<Text>Put your phone number for Login</Text>
<Button title="Login" onPress={getToken} />
</View>
)
}
答案 0 :(得分:0)
异步/等待只是Promises的语法糖。这个:
const handleSignIn = code => {
sign({
variables: { code },
update: async (cache, { data }) => {
const accessToken = data.signIn.accessToken
const refreshToken = data.signIn.refreshToken
await Keychain.setGenericPassword(accessToken, refreshToken)
}
}).then(() => navigation.navigate('Home'))
}
还有这个
const handleSignIn = async (code) => {
await sign({
variables: { code },
update: async (cache, { data }) => {
const accessToken = data.signIn.accessToken
const refreshToken = data.signIn.refreshToken
await Keychain.setGenericPassword(accessToken, refreshToken)
}
})
return navigation.navigate('Home')
}
做同样的事情。异步/等待只是使代码更易于阅读和推理。