source

Angular의 $q.reject() 대 지연.거부()

itover 2023. 2. 11. 09:11
반응형

Angular의 $q.reject() 대 지연.거부()

Angular를 잡으려고 노력 중이야$q서비스 및 관련 개체와 API를 제공합니다.콘솔에서 개체를 보면 다음과 같이 표시됩니다.

var deferred = $q.defer()

...(and then from console inspection)...

$q: Object {defer: function, reject: function, when: function, all: function}

deferred: Object {resolve: function, reject: function, notify: function, promise: Object}

deferred.promise: Object {then: function, catch: function, finally: function}

몇 가지 의문이 생깁니다.

  1. 와의 차이는 무엇입니까?$q.reject()그리고.deferred.reject()? 각각 언제 사용합니까?
  2. 그 사이에 어떤 관계가 있을까요?errorFn…에.deferred.promise.then(successFn, errorFn)및 그catchFn…에.deferred.promise.catch(catchFn)?
  3. 약속들이 잔뜩 들어있었는데 오류가 발생하면 가장 바깥쪽이catch()함수는 항상 호출됩니까?네스트된 약속 중 하나에도 캐치 함수가 정의되어 있다면?그 어획량이 가장 바깥쪽 어획량을 할 수 없게 되는 것입니까?

감사해요.

1)$q.reject()지연을 생성하고 즉시 거부하는 바로 가기입니다.오류를 처리할 수 없는 경우 errorFn에서 자주 사용합니다.

2) 아무것도 없습니다.promise.catch(errorFn)통사당일 뿐이다promise.then(null, errorFn)의 성공 및 오류 방법과 마찬가지로$http다음과 같이 코드를 작성할 수 있습니다.

promise.
    then(function(result){
        // handle success
        return result;
    }, function errorHandler1(error){
        // handle error, exactly as if this was a separate catch in the chain.

    }).catch(function errorHandler2(error){
        // handle errors from errorHandler1
    });

3) 이것이 바로 $q.reject가 도움이 되는 부분입니다.

promise.
    catch(function(error){
       //Decide you can't handle the error
       return $q.reject(error); //This forwards the error to the next error handler;
    }).catch(function(error){
       // Here you may handle the error or reject it again.
       return 'An error occurred'; 
       //Now other errorFn in the promise chain won't be called, 
       // but the successFn calls will.
    }).catch(function(error){
       // This will never be called because the previous catch handles all errors.
    }).then(function(result){
       //This will always be called with either the result of promise if it was successful, or 
       //'An error occured' if it wasn't
    });

좋아, 이건 내 약속이야.

  1. $q.reject(reason)거부된 약속과 함께 인수로서 전달되어 거부된 이유를 반환합니다.Reject는 프로세스가 완료되었는지 여부에 관계없이 기존 프로세스를 거부합니다.

  2. errorFn약속이 거부되었을 때 실행되며, 그 주장이 거부된 이유입니다.Catch는 약속 프로세스 내의 오류가 적절하게 처리되지 않아 약속의 제기 및 예외가 발생하고 거부되거나 이행되지 않을 때 호출됩니다.

  3. 네스트된 약속이 없어야 합니다.이 경우 최신 catch 블록이 처리하도록 지정된 다른 블록이 없는 경우 그 이전의 모든 것을 캐치해야 합니다.

언급URL : https://stackoverflow.com/questions/24443733/angulars-q-reject-vs-deferred-reject

반응형