React Native Javascript: Unhandled Promise Rejection Warning

While working with javascript promises, I have been getting erors like:

UnhandledPromiseRejectionWarning: unhandled Promise Rejection (rejection id:1)

Typically this is a good hint that error handling is not implemented properly for your promise. To make it simpler, here is quick guide on Javascript promise and right way to error handling.

A promise is a good way to do async processing in javascript and provides an alternate to standard events/handlers (for scala developers: promises are like futures in scala).

A promise can be in following states:

  • fulfilled - action relating to the promise succeeded
  • rejected - action relating to the promise failed
  • pending - promise has not been fulfilled or rejected yet
  • settled - promise has been fulfilled or rejected

From error handling perspective, the standard flow is:

var pr1 = new Promise(...) pr1 .then(function(){...}) .catch(function(){...}) .finally(function(){...})

To avoid UnhadledPromiseRejectionWarning, you need to make sure that you implement catch handlers for your promise.