I just want to ask how should I pass the resolve promise to catch
if the value on the resolve is not intended.
e.g.
let prom = getPromise();
prom.then(value => {
if (value.notIWant) {
// Send to catch <-- my question is here, I want to pass it on the catch.
}
// Process data.
}).catch(err => {
// Pass the error through ipc using json, for logging.
});
I tried to using throw
but the object cant be parsed to json and just got an empty object.
ANSWER:
@BohdanKhodakivskyi first ment below is the answer I want.
@31py answer is also correct but the @BohdanKhodakivskyi solution is much simplier and will render the same result.
I just want to ask how should I pass the resolve promise to catch
if the value on the resolve is not intended.
e.g.
let prom = getPromise();
prom.then(value => {
if (value.notIWant) {
// Send to catch <-- my question is here, I want to pass it on the catch.
}
// Process data.
}).catch(err => {
// Pass the error through ipc using json, for logging.
});
I tried to using throw
but the object cant be parsed to json and just got an empty object.
ANSWER:
@BohdanKhodakivskyi first ment below is the answer I want.
@31py answer is also correct but the @BohdanKhodakivskyi solution is much simplier and will render the same result.
Share Improve this question edited Dec 28, 2017 at 8:29 zer09 asked Dec 28, 2017 at 7:53 zer09zer09 1,5962 gold badges33 silver badges50 bronze badges 4-
1
Have you tried
throw value;
? – Bohdan Khodakivskyi Commented Dec 28, 2017 at 7:57 - Also check this question: stackoverflow./questions/33445415/… – Bohdan Khodakivskyi Commented Dec 28, 2017 at 7:59
- 1 @BohdanKhodakivskyi thanks, works like a charm. – zer09 Commented Dec 28, 2017 at 8:07
- Glad it did the job. I added it to the answers – Bohdan Khodakivskyi Commented Dec 28, 2017 at 8:52
4 Answers
Reset to default 6Simply use throw value;
. In your case:
prom.then(value => {
if (value.notIWant) {
// Send to catch
throw value;
}
// Process data.
}).catch(err => {
// Pass the error through ipc using json, for logging.
});
Please also note the difference and limitations between using Promise.reject()
and throw
which is perfectly described in this question. For example, throw
will not work in some async
scenarios.
You can simply return a rejected promise:
prom.then(value => {
if (value.notIWant) {
return Promise.reject('your custom error or object');
}
// Process data.
}).catch(err => {
console.log(err); // prints 'your custom error or object'
});
.catch
actually handles any promise rejection in the chain, so if you're returning a rejected promise, the control automatically flows to catch
.
why you just not rethrow the error? throw new Error("something");
You can use outside functions
to do it:
var processData = function(data) {
// process data here
}
var logIt = function(data) {
// do logging here..
}
let prom = getPromise();
prom.then(value => {
if (value.notIWant) {
// Send to catch <-- my question is here, I want to pass it on the catch.
logIt(/*pass any thing*/);
}
// Process data.
processData(data);
}).catch(err => {
logIt(/*pass any thing*/);
});