A
A
Andrew2016-09-09 11:09:25
JavaScript
Andrew, 2016-09-09 11:09:25

How to catch various rejects in a promise and break the chain?

Please tell me, I'm confused.
Suppose there is some function that returns various states. One state - everything is ok, and different views when something went wrong. You need to run the handler for this state and exit the chain.

function p1(inputData) {
    return new Promise((resolve, reject) => {
        switch(inputData) {
           case SUCCESS_0:
              resolve("Success 0");
              break;
           case ERROR_0:
              reject("Reason 0");
              break;
           case ERROR_1:
              reject("Reason 1");
              break;
           case ERROR_2:
              reject("Reason 2");
              break;
        }
        });
}

you need something like output
p1(inputData)
    .catch(ERROR_0, () => processError_0()) //и выйти из цепочки
    .catch(ERROR_1, () => processError_1()) //и выйти из цепочки
    .catch(ERROR_2, () => processError_2()) //и выйти из цепочки
    .then(ifSuccess_0());  //только если не вылезла ошибка в p1

How can it be gracefully written so that the code works and reads normally?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
_
_ _, 2016-09-10
@AMar4enko

then move to the beginning of the chain, well, on codepen an example of incomprehensible code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question