Reactjs 反应本机生成器功能不工作

Reactjs 反应本机生成器功能不工作,reactjs,react-native,expo,redux-saga,Reactjs,React Native,Expo,Redux Saga,我试图使用生成器函数并想检查令牌。如果存在令牌,则我希望它与console.log一起显示。但是什么都没发生为什么?我也没有错误 getToken.js import * as SecureStore from 'expo-secure-store'; const getSecureStore = async () => { // Check if key already exists const keyExists = await SecureStore.getItemAsy

我试图使用生成器函数并想检查令牌。如果存在令牌,则我希望它与console.log一起显示。但是什么都没发生为什么?我也没有错误

getToken.js

import * as SecureStore from 'expo-secure-store';

const getSecureStore = async () => {
  // Check if key already exists 
  const keyExists = await SecureStore.getItemAsync('uuid');

  return keyExists;
};

function* storeToken() {
  while(true) {
    const keyExists = yield call(getSecureStore());
    console.log(keyExists);
  }
}

export default storeToken;
App.js

import React, { useState } from 'react';
import { NavigationContainer } from '@react-navigation/native';
import AppStack from './src/navigation/stack';
import AppLoading from 'expo-app-loading';
import getSecureKey from './src/utilities/getSecureKey';

const App = () => {
  const [firstContent, setFirstContent] = useState(false);

  const loadContent = async () => {

    await getSecureKey();
  };

  if(firstContent) {
    return (
      <NavigationContainer>
        <AppStack />
      </NavigationContainer>
    )
  } else {
    return (<AppLoading startAsync={loadContent} onFinish={() => setFirstContent(true)} onError={e => console.error(e)} />)
  }
};

export default App;
import React,{useState}来自“React”;
从'@react-navigation/native'导入{NavigationContainer};
从“./src/navigation/stack”导入AppStack;
从“expo应用程序加载”导入应用程序加载;
从“./src/utilities/getSecureKey”导入getSecureKey;
常量应用=()=>{
const[firstContent,setFirstContent]=useState(false);
const loadContent=async()=>{
等待getSecureKey();
};
如果(第一内容){
返回(
)
}否则{
return(setFirstContent(true)}onError={e=>console.error(e)}/>)
}
};
导出默认应用程序;

老实说,我不确定如何以及在何处调用生成器函数。 我希望在某个地方对生成器函数进行.next()调用

但我希望我仍然能够提供帮助。 生成器函数将在关键字“yield”处停止 因此,您的console.log将不会被记录(至少在第一次运行时,直到您使用.next()再次调用它):

我试图简化您的示例: 如您所见,第一个日志没有出现,您正在记录“previous”键!因此,您的console.log()已关闭一次。我们调用了生成器函数三次,但只记录了两次

函数foo(){
返回true;
}
函数*storeToken(){
while(true){
const keyExists=foo();
产量键存在
console.log(keyExists);
}
}
var generatoreStore=storeToken();
GeneratorStore.next();//此处未记录任何内容->未到达console.log
GeneratorStore.next();//日志显示:存在:真

GeneratorStore.next();//出现第二个日志:exists:true
您在哪里使用您的
storeToken
?getSecureKey(),我导出了storeTokenshow
getSecureKey.js
also@localdata01对此有何反馈?
function* storeToken() {
  while(true) {
    const keyExists = yield call(getSecureStore()); // execution stops here
    console.log(keyExists); // will not be called during the first 'call'
  }
}