javascript - 如何从异步函数中提取输出-react-native?

标签 javascript react-native

我有一个问题。 如何从异步函数中获取结果?

这是我的代码:

function kakaoLogin() {
  return async dispatch => {
    RNKakaoLogins.login((err, result) => {
      console.log(result);
    });
    console.log(result);
  };
}

这就是结果。 enter image description here

第一个console.log(result)显示token!。 但第二个 console.log(result) 没有显示任何内容。

我想从第二个 console.log(result) 获得与第一个 console.log(result) 相同的结果

最佳答案

function kakaoLogin() {
  return async dispatch => {
    RNKakaoLogins.login((err, result) => {
      console.log(result);
    });
    console.log(result); // calling result outside its scope will not work
  };
}

您对空结果有何期望?您正在 block 之外使用result。 如果你想做点什么来回应。然后你可以像这样进行方法调用。

function kakaoLogin() {
  return async dispatch => {
    RNKakaoLogins.login((err, result) => {
      console.log(result);
      doSomeWork(result);
      // you can call another method here when you get response.
    });
  };
}

function doSomeWork(result){
.. somework
}

关于javascript - 如何从异步函数中提取输出-react-native?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51786376/

相关文章:

javascript - 如何在 indesign javascript 插件中添加 ListBox(SUI) 或 TreeView(SUI) 的上下文菜单

javascript - 无法加载 base64 图像

scrollview - React Native ScrollView 在发布后重新回到顶部

android - child 在父 View 外不可点击

javascript - Express.js :how to use different middlewares based on NODE_ENV?

javascript - Node.js 导出变量

javascript - React Native 和 Firebase 3

android - 无法使用 ssl url 访问我的服务器

javascript - 文本字符串必须在 React Native 中的 <Text> 内呈现

javascript - 使用颜色选择器使用 jquery 更改颜色?