Axios: μΈν„°μ…‰ν„°μ—μ„œ 약속 체인을 κΉ¨λŠ” 방법은 λ¬΄μ—‡μž…λ‹ˆκΉŒ?

에 λ§Œλ“  2017λ…„ 02μ›” 19일  Β·  3μ½”λ©˜νŠΈ  Β·  좜처: axios/axios

약속 μ‚¬μŠ¬μ„ λŠλŠ” 방법?

instance.interceptors.response.use((response) ->
    # my server returns {"status": "success", "data": ...}
    # or {"status": "fail", "data": ...}
    server_response = response.data
    if server_response.status == 'fail'
        alert(server_response.data)  # global alert when status == 'fail'
        # this will throw a "> Uncaught (in promise) ..."
        # how can i do to prevent/stop it enter into the next then()?
        # only when server_response.status == 'success' enter the `then` function
        return Promise.reject(response)

    # only status == 'success' will reach here:
    return response

# in my button actions
instance
    .post('accounts/login', account)
    .then (response) ->
        # if i don't use Promise.reject() in the interceptors,
        # everytime i will use a if:
        if response.data.status == 'success'
            doThings(response)

        # but i want this
        doThings(response)

각도:
http://blog.zeit.io/stop-a-promise-chain-without-using-reject-with-angular-sq/

λΈ”λ£¨λ²„λ“œμ—μ„œ:
http://openmymind.net/Cancelling-Long-Promise-Chains/

μœ„μ˜ μ˜ˆλŠ” then() μ—μ„œ 체인을 끊고 axiosλŠ” μΈν„°μ…‰ν„°μ—μ„œλ„ λŠμ„ 수 μžˆμŠ΅λ‹ˆλ‹€.

instance.interceptors.response.use((response) ->
    if someCondition(response)
        return null  # break the chain
    else
        return response

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

κ·Έλ ‡κ²Œ ν•˜λŠ” 것은 ꢌμž₯ν•˜μ§€ μ•Šμ§€λ§Œ λ§ˆμΉ¨λ‚΄ μ›ν•˜λŠ” 경우 μΈν„°μ…‰ν„°μ—μ„œ ν•΄κ²°λ˜μ§€ μ•ŠλŠ” 약속을 λ°˜ν™˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 예:

instance.interceptors.response.use((response) =>Β {
  if (someCondition(response) {
    return new Promise(() => {});
  }
  return response;
});

λͺ¨λ“  3 λŒ“κΈ€

κ·Έλ ‡κ²Œ ν•˜λŠ” 것은 ꢌμž₯ν•˜μ§€ μ•Šμ§€λ§Œ λ§ˆμΉ¨λ‚΄ μ›ν•˜λŠ” 경우 μΈν„°μ…‰ν„°μ—μ„œ ν•΄κ²°λ˜μ§€ μ•ŠλŠ” 약속을 λ°˜ν™˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 예:

instance.interceptors.response.use((response) =>Β {
  if (someCondition(response) {
    return new Promise(() => {});
  }
  return response;
});

λ‚˜λŠ” κ²°μ½” ν•΄κ²°λ˜κ±°λ‚˜ κ±°λΆ€λ˜μ§€ μ•ŠλŠ” 약속을 μ‚¬μš©ν•˜λŠ” 아이디어가 λ§ˆμŒμ— 듀지 μ•Šμ•˜κΈ° λ•Œλ¬Έμ— λ‹€μŒκ³Ό 같이 ν•˜κΈ° μœ„ν•΄ λΈ”λ£¨λ²„λ“œμ˜ μ·¨μ†Œλ₯Ό μ‚¬μš©ν•˜κΈ°λ‘œ κ²°μ •ν–ˆμŠ΅λ‹ˆλ‹€.

axios.interceptors.response.use(null, error => {
    let promise = new Promise(resolve, reject) => {
        setTimeout(() => {
            <code>
            promise.cancel()
        })
    })
    return promise
})

setTimeout은 μ μ ˆν•œ 약속이 μ—¬μ „νžˆ μΈν„°μ…‰ν„°μ—μ„œ λ°˜ν™˜λ˜κ³  사싀 이후에 .cancel() κ°€ 호좜될 수 μžˆλ„λ‘ 약속이 자체 μ΄ˆκΈ°ν™”λ˜λ„λ‘ ν—ˆμš©ν•©λ‹ˆλ‹€.

@rubennorte에 νšŒμ‹ :

κ·Έλ ‡κ²Œ ν•˜λŠ” 것은 ꢌμž₯ν•˜μ§€ μ•Šμ§€λ§Œ λ§ˆμΉ¨λ‚΄ μ›ν•˜λŠ” 경우 μΈν„°μ…‰ν„°μ—μ„œ ν•΄κ²°λ˜μ§€ μ•ŠλŠ” 약속을 λ°˜ν™˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 예:

instance.interceptors.response.use((response) => {
  if (someCondition(response) {
    return new Promise(() => {});
  }
  return response;
});

https://stackoverflow.com/a/20068922

κ°„λ‹¨νžˆ λ§ν•΄μ„œ - μ΅œμ†Œν•œ μ΅œμ‹  λΈŒλΌμš°μ €μ—μ„œλŠ” - μ™ΈλΆ€ μ°Έμ‘°κ°€ μ—†λŠ” ν•œ ν•΄κ²°λ˜μ§€ μ•Šμ€ 약속에 λŒ€ν•΄ κ±±μ •ν•  ν•„μš”κ°€ μ—†μŠ΅λ‹ˆλ‹€.

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰