返回值时出错

时间:2018-08-28 11:17:08

标签: reactjs react-native

运行getTicket()

Alert (JSON.stringify (responseJson));

数据已正确恢复 但是在listTicket()中,其他数据显示给了我 我的代码如下

getTicket:

async getTicket ()
    {
      try {
        let response = await fetch(
          'url'
        ,{
          method: 'POST',
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({
            uid: '1',
            type: '0',
          })
        });

        let responseJson = await response.json();
        alert(JSON.stringify(responseJson));
        return responseJson;
      } catch (error) {
        alert(error);
      }

    }

listTicket代码:

listTicket()
   {

     let objectTicket = this.getTicket();


     alert(JSON.stringify(objectTicket));
   }

请帮助

1 个答案:

答案 0 :(得分:1)

只需像这样调用this.getTicket()即可从异步函数中返回承诺:

this.getTicket()()
  .then(function(objectTicket) {
    alert(JSON.stringify(objectTicket));
  });