Possible Unhandled Promise Rejection (id:0) Warning

强颜欢笑 提交于 2019-11-30 19:16:12

问题


I am getting the following warning message when my AsyncStorage Item is empty "Possible Unhandled Promise Rejection (id:0)" So my question is: How can I handle a promise rejection?

My code:

componentDidMount() {
        try {
            // This warning only appears when 'connections' item is empty
            AsyncStorage.getItem('connections').then((token) => {
                token = JSON.parse(token);

                const getSectionData = (dataBlob, sectionId) => dataBlob[sectionId];
                const getRowData = (dataBlob, sectionId, rowId) => dataBlob[`${rowId}`];

                const ds = new ListView.DataSource({
                    rowHasChanged: (r1, r2) => r1 !== r2,
                    sectionHeaderHasChanged: (s1, s2) => s1 !== s2,
                    getSectionData,
                    getRowData,
                });

                const {dataBlob, sectionIds, rowIds} = this.formatData(token);

                this.setState({
                    dataSource: ds.cloneWithRowsAndSections(dataBlob, sectionIds, rowIds),
                });
            });
        }catch(error) {
            console.log(error);
        }
    }

回答1:


You need to catch the reject of the promise:

componentDidMount() {
  // This warning only appears when 'connections' item is empty
  return AsyncStorage.getItem('connections').then((token) => {
    token = JSON.parse(token);

    const getSectionData = (dataBlob, sectionId) => dataBlob[sectionId];
    const getRowData = (dataBlob, sectionId, rowId) => dataBlob[`${rowId}`];

    const ds = new ListView.DataSource({
      rowHasChanged: (r1, r2) => r1 !== r2,
      sectionHeaderHasChanged: (s1, s2) => s1 !== s2,
      getSectionData,
      getRowData,
    });

    const { dataBlob, sectionIds, rowIds } = this.formatData(token);

    this.setState({
      dataSource: ds.cloneWithRowsAndSections(dataBlob, sectionIds, rowIds),
    });
  }).catch(error => {
    console.log(error);
  })
}


来源:https://stackoverflow.com/questions/47936361/possible-unhandled-promise-rejection-id0-warning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!