Axios: كيف تكسر سلسلة الوعد في الاعتراضات؟

تم إنشاؤها على ١٩ فبراير ٢٠١٧  ·  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() ، يمكن أن تكسرها المحاور أيضًا في المعترض ، شيء مثل:

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 التقييمات