. Advertisement .
..3..
. Advertisement .
..4..
When operating the AsyncStorage Item, you suddenly find this warning “Possible Unhandled Promise Rejection (id:0)”. This is a common error in Javascript that you may face. In this post, we will instruct how to fix this warning. Please follow the below part to get the cause and solution for this error.
When does it occur?
It is aware that the items in the connection are unfilled and the “Possible Unhandled Promise Rejection (id:0)” error is appearing. If a promise is dropped and you haven’t corrected it, it’s “unfixed”. You have not a try… catch a block in the code because you try to promise the result from the AsyncStorage call.
Here is 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);
}
}
How to correct the “Possible Unhandled Promise Rejection (id:0)” issue?
If you don’t use await, try/catch won’t work on promises. There is only one way to settle the rejection’s promise and have to catch blocks where the promise is getting back. So, you remember to ‘catch’ if you have any errors which might be returned and resolve them. See the correct code below:
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);
})
}
Conclusion
The solution mentioned above is the fastest method for those who are still not sure with this problem “Possible Unhandled Promise Rejection (id:0)”, can catch it up. Don’t worry; we will support you if you have trouble fixing this issue. Finally, we wish you an amazing day with your subjects. Also, if you have further questions, feel free to chat with us. Thank you for reading our post!
Another way for you to still get the same results. Use react-native-cli. I used this program, and it works for me: