Reputation: 151
I'm currently writing an Ionic app and I would need to chain down error, but somehow I'm keep getting this
ERROR Error: Uncaught (in promise): [object Object]
when I try to reject the error. I suppose I must have been doing something wrong somewhere but I don't know how to do it. The error supposedly is only handled at the doSomething() method.
The structure of the code is like this:
sendRequest(...) {
...
return new Promise(resolve => {
this.http.post(this.server, body, {headers: header, withCredentials: true}).subscribe(data => {
resolve(data);
}, err => {
return Promise.reject(err);
});
});
}
procesRequest(...) {
...
return new Promise(resolve => {
this.sendRequest(...).then((data) => {
resolve(data);
}, (reason) => {
return Promise.reject(reason);
}).catch((error => {
return Promise.reject(error);
}));
});
}
doSomething {
...
processRequest(...).then((data) => {
this.showSuccessMessage();
}, (err) => {
this.showErrorMessage(err);
}).catch((err) => {
this.showErrorMessage(err);
});
}
Upvotes: 1
Views: 349
Reputation: 1037
The basic example is something like below:
var promise1 = new Promise(function(resolve, reject) {
var cleanRoom = false;
if(!cleanRoom){
reject('promise1 rejected');
}
});
var promise2 = new Promise(function(resolve, reject) {
if(true){
reject('promise2 rejected');
}
});
function doSomething () {
promise1.catch((rej) => {
console.log(rej);
});
promise2.catch((rej) => {
console.log(rej);
});
}
doSomething();
Upvotes: 1