I\'m trying to use the routerCanDeactivate
function for a component in my app. The simple way to use it is as follows:
routerCanDeactivate() {
Is there a reason why there is an error saying that there is no 'resolve' property on Promise?
Yes, and it's that tsc can't find the correct typings for es6-promise
. To avoid this and other typing problems in ng2 projects, as of beta.6 you need to explicitly include
///
somewhere in your application (typically this is done at the top of your main bootstrap file).*
The rest of your question is less clear (and is likely an XY problem where x is the typings problem discussed above). But, if I'm correct in understanding that you've defined a promise like this:
private promise: Promise = new Promise(
( resolve: (res: boolean)=> void, reject: (res: boolean)=> void) => {
const res: boolean = false;
resolve(res); // <=== how would this ever resolve to anything but false???
}
);
How are you expecting this to resolve to anything but false?
const res: boolean = false;
resolve(res); //always false
is equivalent to
resolve(false); //always false
*note: this is (presumably) temporary, and won't be necessary in later beta/release versions.
Update in response to your comment:
it doesn't seem obvious how I can wait for the handleResponse function to run and wait for that response
I'm still not clear on what you're trying to do here, but in general, you'd want to have handleResponse
return a promise of its own, and then:
private promise: Promise = new Promise((resolve, reject) => {
handlePromise.then(resultOfHandleResult => {
//assuming you need to process this result somehow (otherwise this is redundant)
const res = doSomethingWith(resultOfHandleResult);
resolve(res);
})
});
handleResponse(res: any) {
this.promise.then(val => {
val = res;
});
}
Or, (far) more preferably, use Observables:
var promise = handleResult() //returns an observable
.map(resultOfHandleResult => doSomethingWith(resultOfHandleResult))