最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to send value from Promise 'then' to 'catch'? - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

4 Answers 4

Reset to default 6

Simply 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*/);
});
发布评论

评论列表(0)

  1. 暂无评论