Web Dev/1. JS 문법 관련

Promise reject 의 처리 방법

hYhY1234 2021. 2. 11. 17:23
728x90

developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject

 

Promise.reject() - JavaScript | MDN

The Promise.reject() method returns a Promise object that is rejected with a given reason. The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://githu

developer.mozilla.org

L.filter = curry(function* (f, iter) {
  for (const a of iter) {
    const b = convertPromise(a, f); // then은 다시 promise를 반환한다.
    log(b); // b가 promise로 오면 무조건 promise라 통과되는거고 아직 값이 반영이 안된다
    // then을 통해서 값을 풀어낸다. 아니면 안흘러가도록 kleslie 컴포지션을 활용한다
    if (b instanceof Promise)
      yield b.then((b) => (b ? a : Promise.reject("없다")));
    if (b) yield a;
  }
});

이 코드를 보면 filter할때 해당하지 않는경우에는 reject를 보내는데, 이렇게 되면 그다음에 then 객체들이 흘려보낸다. 

 

 

then(resolve 된거 처리, reject된거 처리)

이런 구조인데, reject된거로 처리 안된건 마지막에 catch로 처리해주거나 할 수 있다. 

 

go(
  [1, 2, 3, 4, 5, 6],
  //
  L.map((a) => Promise.resolve(a * a)),
  L.filter((a) => a % 2),
  L.map((a) => {
  log("reject", a);
  return Promise.resolve(a + 10);
  }),
  takeAll,
  (res) => log("klesili", res)
).catch((err) => console.log("error", err));