Components.lastResult returns the numeric nsresult code that was the result code of the last XPCOM method called via XPConnect.


Generally, Components.lastResult is only useful for testing the result of XPCOM methods that can return interesting 'success' codes. This is because failure result codes get converted by XPConnect into exceptions that are thrown into the calling JavaScript method. Most interfaces only return one success code -- NS_OK -- so Components.lastResult is rarely necessary.

In cases where Components.lastResult is used, it is best to get it right away after the target call is made and save it in a local variable to test its value rather than doing multiple tests against Components.lastResult. This is because many 'Components' properties and methods are themselves implemented using XPConnect and subsequent calls to Components.lastResult might reflect the result of 'implicit' XPConnect calls rather than the result of the target call.


In the following example, the local variable i contains the actual result returned by bar() (assuming that bar() is called via XPConnect), and Components.lastResult contains the success code returned by bar().

// Given that is a method that might return 
// the success codes NS_OK, '5', and '6' OR some error code...
  var i =;

  switch (Components.lastResult)
    case Components.results.NS_OK:
      // NS_OK is good!
    case 5:
      // do something with 5 here
    case 6:
      // do something with 6 here
      // this was a success code we did not expect. Bad component!
  // and so on....
catch (e)
  // the call threw an exception or a native component returned 
  // a failure code!
  if (e instanceof Components.interfaces.nsIXPCException)
    // we might do something interesting here with the exception object
    var rv = e.result;
    // if we don't know how to handle it then rethrow
    throw e;

Document Tags and Contributors

 Contributors to this page: teoli, Mgjbot, Waldo, Andreas Wuest
 Last updated by: Mgjbot,