0
componentDidMount(){

    console.log("get")
    AsyncStorage.getItem('token')
                    .then((res)=>{
                console.log(res)    
            })

}

I have componentDidMount with console.log and AsyncStorage.getItem function.I want to make axios call after getting the token value.
But the thing is AsyncStorage.getItem is not working get //console.log('get') is printed as log without res.
Any help please how to get value? am I doing something wrong?

2
  • I would start by adding a .catch() .method to your promise chain to see if you are getting any errors. Commented Sep 10, 2018 at 20:02
  • @ mattdevio I added .catch() now but no error is printed in that. Commented Sep 10, 2018 at 20:10

2 Answers 2

2

You can do it as follows.

componentDidMount(){
    this.onGetToken();
}
onGetToken = async () => {
    try {
      const token = await AsyncStorage.getItem('token');
      console.log("get")
      // call api using axios with token value
    } catch(e => console.info('error for getting token', e))
}
Sign up to request clarification or add additional context in comments.

8 Comments

If the getItem() method returns a promise then both your code and his are identical in terms of behavior.
that's why we use await keyword.
Which is why your code won't solve his problem. Its the same functionality.
I updated the answer, I think the current token value is null or undefined.
Thats possible.
|
0

Try to Just restart the app and asyncStorage will behave properly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.