μ½μ μ¬μ¬μ λλ λ°©λ²?
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;
});
λλ κ²°μ½ ν΄κ²°λκ±°λ κ±°λΆλμ§ μλ μ½μμ μ¬μ©νλ μμ΄λμ΄κ° λ§μμ λ€μ§ μμκΈ° λλ¬Έμ λ€μκ³Ό κ°μ΄ νκΈ° μν΄ λΈλ£¨λ²λμ μ·¨μλ₯Ό μ¬μ©νκΈ°λ‘ κ²°μ νμ΅λλ€.
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
κ°λ¨ν λ§ν΄μ - μ΅μν μ΅μ λΈλΌμ°μ μμλ - μΈλΆ μ°Έμ‘°κ° μλ ν ν΄κ²°λμ§ μμ μ½μμ λν΄ κ±±μ ν νμκ° μμ΅λλ€.
κ°μ₯ μ μ©ν λκΈ
κ·Έλ κ² νλ κ²μ κΆμ₯νμ§ μμ§λ§ λ§μΉ¨λ΄ μνλ κ²½μ° μΈν°μ ν°μμ ν΄κ²°λμ§ μλ μ½μμ λ°νν μ μμ΅λλ€. μ: