I cannot find a way to catch, at the top level, exceptions that might occur deep in a promise fulfillment chain. Here is the sample code that I've written and two different approaches, neither of which works:
import * as Q from 'q';
function main()
{
console.log("In main now.");
One().then(function()
{
console.log("One's promise fulfilled.");
})
.catch(function()
{
console.log("One's promise rejected.");
})
}
function One() : Q.Promise<void>
{
var deferred = Q.defer<void>();
Two().then(function()
{
console.log("Two's promise fulfilled.");
this.foo();
deferred.resolve();
})
return deferred.promise;
}
function Two() : Q.Promise<void>
{
var deferred = Q.defer<void>();
deferred.resolve();
return deferred.promise;
}
main();
The alternative I've tried is to use an onReject handler like so:
function main()
{
console.log("In main now.");
One().then(function()
{
console.log("One's promise fulfilled.");
},
function()
{
console.log("One's promise rejected.");
})
}
In neither approach am I able to catch the exception that occurs at line:
this.foo();
In other words, neither approach generates the "One's promise rejected" output to the console. Is there any way to catch such exceptions at the top level?