0

I am facing problem returning value from a function in react native. It is a global function , not inside any class. I am assigning return value to a var. But value is always undefined.Below is function code snippet.

var userLoggedIn= (function (){
    AsyncStorage.getItem("isLoggedIn").then((login) => {
        var loggedIn = login;
        console.log("Logged In " + loggedIn); // getting correct value
        return loggedIn;
    }).done();
})();

console.log("userLoggedIn" + userLoggedIn); // this is undefined

const IntialScreen = AppNavigator(loggedIn); export default IntialScreen;

Please check what I am doing wrong here. I am new to react native and JavaScript, so I may be doing something wrong or I am not aware about some concept of JavaScript

Paras Watts
  • 2,565
  • 4
  • 21
  • 46
  • 2
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Mayank Shukla Aug 14 '17 at 06:15

2 Answers2

1

you should return a promise when handling async calls. in your case.userLoggedIn is undefined because it gets executed without waiting for the response that AsyncStorage.get item() is returning

var userLoggedIn= (function (){
      return new Promise((resolve, reject) => {
         AsyncStorage.getItem("isLoggedIn").then((login) => {
           var loggedIn = login;
           console.log("Logged In " + loggedIn); // getting correct value
           resolve(loggedIn);
        }).done();
      })

})();

updated based on clarification by OP

export default function(callback){
  userLoggedIn.then((loggedIn)=>{
    //do whatever you want to do with it
   const IntialScreen = AppNavigator(loggedIn);
   callback(IntialScreen);

  })
};

some component

 import InitialScreen from './somefile';
   InitialScreen((loginData)=>{
     //loginData will have what you wanted to export 
   });
hannad rehman
  • 4,133
  • 3
  • 33
  • 55
  • I want to pass login value to Stack navigator const IntialScreen = AppNavigator('login value'); export default IntialScreen; How do i do that ? – Paras Watts Aug 14 '17 at 07:10
  • i will try to explain it. when handling async calls and updating variables, you may not be able to use the variable directly because its value update is indefinite, it can happen anytime. so you need to resolve or in simple terms provide a functionality that this async call has finished its execution. the final result can be a success or failure. success is handeled by resolve(val) and failure in reject(val). https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise http://www.mattgreer.org/articles/promises-in-wicked-detail/ here are some references @ParasWatts – hannad rehman Aug 14 '17 at 08:53
  • How do I pass this value to StackNavigator which in my case I have declared as AppNavigator. In .then I have tried to assign loggedIn to some variable, but again that variable is undefined when I pass it to App navigator – Paras Watts Aug 14 '17 at 09:00
  • you do all your computation and manipulation in the then block of user loggedIn function . userLoggedIn.then((loggedIn)=>{ //do whatever you want to do with it //loggedIn param is your final variable })@ParasWatts – hannad rehman Aug 14 '17 at 09:03
  • But I can not export a component there which I want to do finally and pass that value . Please check my updated question what I want to do with my loggedIn variable – Paras Watts Aug 14 '17 at 09:04
  • buddy i got your problem. you cant export default it. in this case i can only think of a callback function which might help you. i will update the answer – hannad rehman Aug 14 '17 at 09:08
  • Ok, I will wait for your answer. Thanks – Paras Watts Aug 14 '17 at 09:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/151870/discussion-between-paras-watts-and-hannad-rehman). – Paras Watts Aug 14 '17 at 09:25
0

var userLoggedIn= false;

(function (){

AsyncStorage.getItem("isLoggedIn").then((login) => {
    var loggedIn = login;
    console.log("Logged In " + loggedIn); // getting correct value
}).done(function() {
    userLoggedIn= loggedIn;
 });

})();

Hope this helps

shinuq
  • 16
  • OP wants to return from the function – hannad rehman Aug 14 '17 at 06:31
  • I think @paras need the variable userLoggedIn to have the user state of login. var userLoggedIn= false; (function (){ AsyncStorage.getItem("isLoggedIn").then((login) => { var loggedIn = login; console.log("Logged In " + loggedIn); // getting correct value }).done(function() { userLoggedIn= loggedIn; }); })(); – shinuq Aug 14 '17 at 07:18
  • correct, but if the variable is inside the function it is of no use. you cant use it outside when its value is update in async manner – hannad rehman Aug 14 '17 at 08:49