0
var data = 0;    
NativeStorage.getItem('loginSession',(result)=>{
    data = result;
},(err)=>{ 
    console.log(err);
});
// I want here the new value of variable data

Just like I mentioned here, How to get the value at the end of the function. As Because I am requesting this function from another to get that particular value.. Like this

function a(){
    var ntval = getmetheNativeValue();
    console.log(ntval);
}

function getmetheNativeValue(){
    var data = 0;    
    NativeStorage.getItem('loginSession',(result)=>{
        data = result;
//And here if I try to return the data variable like return(data); not working as well 
    },(err)=>{ 
        console.log(err);
    });
//now here I need to return the new data value like this return(data); but this returning the old value as 0 not the new value.
}

Please any one tell me how to get the value in first function from the second one?

here is my asn function pleaes check whether I am going in the right direction or not

itemInCart:async(id)=>{ 

        await NativeStorage.getItem('cartSession',(result)=>{ 

            if(result==''){
                returns= 0;
                //return returns;
            }else{
                var split = result.split(';');

                for(var i=0;i<split.length;i++){
                    var split_again = split[i].split('-');
                    if(split_again[0]==id){ console.log('true event');
                         returns= 1;

                        //return returns;
                    }
                }
                 returns= 0;

                //return returns;
            }
        },()=>{
             returns= 0;

            //return returns;
        });
      return returns;
    }
Nayanendu Mondal
  • 53
  • 1
  • 1
  • 8
  • Why don't you `return` it? – Islam Elshobokshy Jan 11 '19 at 16:04
  • @IslamElshobokshy No returning is not helping either in the inner nor out side of that function – Nayanendu Mondal Jan 11 '19 at 16:15
  • 2
    `NativeStorage.getItem` is asynchronous. You need to use Promise or async/await. – marekful Jan 11 '19 at 16:17
  • 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) – freedomn-m Jan 11 '19 at 16:20
  • Can you please chek my last edit of async function, is it the right way to do so?? @freedomn-m – Nayanendu Mondal Jan 11 '19 at 17:13
  • we simply DO NOT include any async function inside sync function! In your example, console.log(ntval) runs, before getmetheNativeValue() finishes! You should replace data=result by console.log(result) inside getmetheNativeValue(), and remove console.log(ntval) in a(), then all codes will be run in right order. – Kenneth Li Jan 13 '19 at 13:20

0 Answers0