admin管理员组

文章数量:1295847

my problem is i want to get a json from this function but all i get is a only a promise and why i choose this way because in app that i'm working on it works dynamicly but the only place that i see i can put this promise is render()

this is my code :

var userInfo = (async()=>{
    var value = await AsyncStorage.getItem("@user_info");
    value = JSON.parse(value);
    return value ;
  })();

and this is my result :

Promise {
 "_40": 0,
 "_55": null,
 "_65": 0,
 "_72": null,
}

but what i want to get is a json what sould i have to do ?

my problem is i want to get a json from this function but all i get is a only a promise and why i choose this way because in app that i'm working on it works dynamicly but the only place that i see i can put this promise is render()

this is my code :

var userInfo = (async()=>{
    var value = await AsyncStorage.getItem("@user_info");
    value = JSON.parse(value);
    return value ;
  })();

and this is my result :

Promise {
 "_40": 0,
 "_55": null,
 "_65": 0,
 "_72": null,
}

but what i want to get is a json what sould i have to do ?

Share Improve this question asked Nov 13, 2017 at 21:43 shahabvshahabishahabvshahabi 9552 gold badges7 silver badges18 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

You have to call this function from ponentDidMount and after the promise pletes, call setState.

Here is a canonical example of how to do this:

class User extends React.Component {
  state = { user: null };

  render() {
     return this.state.user && <div>{this.state.user.name}</div>;
  }

  async ponentDidMount() {
    const value = await AsyncStorage.getItem("@user_info");
    if (!this._unmounted) {
      const user = JSON.parse(value);
      this.setState({ user: user });
    }
  }

  ponentWillUnmount() {
    this._unmounted = true;
  }
}

You should return value.json() . Try this

var userInfo = (async()=>{
    var value = await AsyncStorage.getItem("@user_info");
    return value.json() ;
  })();

In addition, AsyncStorage.getItem is an asynchronous function, you will need to wrap that in try and catch in case if any error happened during the process

Note: If you use an await inside a function that is not explicitly declared with async, you'll end up with an Unexpected token syntax error.

async userInfo(){
    try{
        var value = await AsyncStorage.getItem("@user_info");
        return value.json() ;
    }
    catch(e){
        console.log('caught error', e);
        // Handle exceptions
    }
}

I think it is because your function is self-executing.

You should be able to access the value if you remove the self-execution

var userInfo = async () => {
    var value = await AsyncStorage.getItem("@user_info");
    value = JSON.parse(value);
    return value ;
  });

As AsyncStore.getItem is a Promise you could also use .then method

const userInfo = () => {
  AsyncStorage.getItem("@user_info").then((response) => {
     console.log(response);
  });
}

本文标签: javascriptreact native how to get return from a promiseStack Overflow