【问题标题】:how can i use async await in regular order?我如何按常规顺序使用异步等待?
【发布时间】:2021-04-09 22:14:08
【问题描述】:

当我在按下时 onPress={() => Login()} 函数

首先,我想通过使用 signInWithKakao 函数来获取令牌, 其次,在我获得令牌之后,如果我有令牌,我想通过使用发送配置文件 kakaoprofile函数

如果我在按下按钮时使用我的代码,则会发生此错误

  token is not defined 

我认为是因为signInWithKakao在登录功能中无法识别。

如何修复我的代码?

这是我的代码

import {
  getProfile as getKakaoProfile,
  login,
} from '@react-native-seoul/kakao-login';

const Kakao = () => {

  const [result, setResult] = useState('');

  const Login = () => {

    const signInWithKakao = async() => {
      const token = await login();
      setResult(JSON.stringify(token));
    };

    if (token) {
      const kakaoprofile = async() => {
        const profile = await getKakaoProfile();
        // console.log("profile:",profile);
        dispatch({
          type: KAKAOLOG_IN_REQUEST,
          data: profile
        })
      };
    }
  }

  return (
    <Container>
      <LoginButton onPress = {() => Login()}>
        <Label>카카오 로그인</Label>
      </LoginButton>
    </Container>
  );
};

export default Kakao;

【问题讨论】:

  • 现在有什么问题?错误是什么?
  • @Konstantin 我因错误更新了我的帖子

标签: javascript node.js reactjs react-native


【解决方案1】:

您可以尝试创建另一个函数。假设 handleOrder 并使其异步,然后将所有等待放在那里。


const Login = () => {
  handleInOrder()
}

async function handleInOrder() {
try{
  const token = await getKakaoProfile()
  if(token){
   let data = await nextInOrder(token)
   //do a dispatch
   }
} catch(err){
  // handle the error
}
}


如果您需要任何帮助,请告诉我

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    • 2022-06-19
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    相关资源
    最近更新 更多