Javascript JS布尔函数在完成之前返回false(react native app)

Javascript JS布尔函数在完成之前返回false(react native app),javascript,react-native,react-redux,react-navigation,Javascript,React Native,React Redux,React Navigation,我正在尝试使用react native在我的应用程序中实现FB登录功能。我面临的一个问题是,当我检查是否存储了fb令牌时,react navigator的初始化不会等待函数完成。我之所以知道这一点,是因为我在isLoggedIntoFB()返回结果之前,在导航初始化的调试器语句中看到。我很困惑,因为我以为我是在以同步方式调用我的函数 auth/index.js: const FBSDK = require('react-native-fbsdk'); const { AccessToken,

我正在尝试使用react native在我的应用程序中实现FB登录功能。我面临的一个问题是,当我检查是否存储了fb令牌时,react navigator的初始化不会等待函数完成。我之所以知道这一点,是因为我在isLoggedIntoFB()返回结果之前,在导航初始化的调试器语句中看到。我很困惑,因为我以为我是在以同步方式调用我的函数

auth/index.js:

const FBSDK = require('react-native-fbsdk');
const {
  AccessToken,
    LoginManager,
} = FBSDK;
export const isLoggedIn = () => {

if(isLoggedIntoFB()) {
    return true;
} else {
    // returning false even though isLoggedIntoFB() prints "tokenFound" 
    // statement later
    return false;
}
}

export const OnLogout = () => {
 alert("Logging out");
logoutFB();
}



function isLoggedIntoFB() {
    alert("isLoggedIn");
AccessToken.getCurrentAccessToken().then(
            (data) => {
                console.log(data);
            if(data.accessToken != null){
                console.log("Token Found")
                return true;
            } else {
                console.log("No previous token found")

                return false;
            }


            }
        );
}

logoutFB = () => {
LoginManager.logOut();
//AccessToken.setCurrentAccessToken(null);
}
import { combineReducers } from 'redux';
import { NavigationActions } from 'react-navigation';
import { AppNavigator } from '../navigators/AppNavigator';
import { isLoggedIn } from "../auth";



var initialNavState = (function() {
if(isLoggedIn() ) { // immediately evaluates to false
    console.log("LoggedIn!!!");
return AppNavigator.router.getStateForAction(NavigationActions.reset({
    index: 0,
    actions: [
      NavigationActions.navigate({
        routeName: 'LoggedIn',
      }),
    ],
}));
} else {
    console.log("not Logged In");
return AppNavigator.router.getStateForAction(NavigationActions.reset({
  index: 1,
  actions: [
    NavigationActions.navigate({ routeName: 'LoggedIn'}),
    NavigationActions.navigate({ routeName: 'LoggedOut'})
  ]
}));
}
}
) ();




function nav(state = initialNavState, action) {
  let nextState;
  switch (action.type) {
    case 'LOGIN':
      nextState = AppNavigator.router.getStateForAction(
        NavigationActions.back(),
        state
      );
      break;

    case 'LOGOUT':
      nextState = AppNavigator.router.getStateForAction(
        NavigationActions.navigate({ routeName: 'LoggedOut' }),
        state
      );
      break;
    default:
      nextState = AppNavigator.router.getStateForAction(action, state);
      break;
  }

  // Simply return the original `state` if `nextState` is null or undefined.
  return nextState || state;
}
reducer/index.js:

const FBSDK = require('react-native-fbsdk');
const {
  AccessToken,
    LoginManager,
} = FBSDK;
export const isLoggedIn = () => {

if(isLoggedIntoFB()) {
    return true;
} else {
    // returning false even though isLoggedIntoFB() prints "tokenFound" 
    // statement later
    return false;
}
}

export const OnLogout = () => {
 alert("Logging out");
logoutFB();
}



function isLoggedIntoFB() {
    alert("isLoggedIn");
AccessToken.getCurrentAccessToken().then(
            (data) => {
                console.log(data);
            if(data.accessToken != null){
                console.log("Token Found")
                return true;
            } else {
                console.log("No previous token found")

                return false;
            }


            }
        );
}

logoutFB = () => {
LoginManager.logOut();
//AccessToken.setCurrentAccessToken(null);
}
import { combineReducers } from 'redux';
import { NavigationActions } from 'react-navigation';
import { AppNavigator } from '../navigators/AppNavigator';
import { isLoggedIn } from "../auth";



var initialNavState = (function() {
if(isLoggedIn() ) { // immediately evaluates to false
    console.log("LoggedIn!!!");
return AppNavigator.router.getStateForAction(NavigationActions.reset({
    index: 0,
    actions: [
      NavigationActions.navigate({
        routeName: 'LoggedIn',
      }),
    ],
}));
} else {
    console.log("not Logged In");
return AppNavigator.router.getStateForAction(NavigationActions.reset({
  index: 1,
  actions: [
    NavigationActions.navigate({ routeName: 'LoggedIn'}),
    NavigationActions.navigate({ routeName: 'LoggedOut'})
  ]
}));
}
}
) ();




function nav(state = initialNavState, action) {
  let nextState;
  switch (action.type) {
    case 'LOGIN':
      nextState = AppNavigator.router.getStateForAction(
        NavigationActions.back(),
        state
      );
      break;

    case 'LOGOUT':
      nextState = AppNavigator.router.getStateForAction(
        NavigationActions.navigate({ routeName: 'LoggedOut' }),
        state
      );
      break;
    default:
      nextState = AppNavigator.router.getStateForAction(action, state);
      break;
  }

  // Simply return the original `state` if `nextState` is null or undefined.
  return nextState || state;
}

你可以使用回调。刚刚将下一个函数传递给必备函数。完成所需操作后,只需调用回调函数。
isLoggedIntoFB
是异步的-它发出一个稍后获取数据的请求,而不是立即获取数据,您必须等待响应-它不能同步(即立即)返回
true
false
-一直最受欢迎的dupe,dupe,你们能告诉我怎么做吗?那个么你们可以使用callback。刚刚将下一个函数传递给必备函数。完成所需操作后,只需调用回调函数。
isLoggedIntoFB
是异步的-它发出一个稍后获取数据的请求,而不是立即获取数据,您必须等待响应-它不能同步(即立即)返回
true
false
-一直以来最受欢迎的傻瓜你能告诉我怎么做吗?