Handling database errors to collect multiple collections

I retrieve multiple collections in the trunk using the invdescore invoke method

var cols = [kf.Collections.invoices, kf.Collections.quotes, kf.Collections.bankaccounts, kf.Collections.invoicepaymentmethods];

var colsComplete = _.invoke(cols, 'fetch');
$.when.apply(null, colsComplete).then(callback);

The problem I encountered, even if the error retrieving to a specific collection due to an API error, resumes fetching the remaining collections.

Is there a way to stop fetching other collections if my API returns "UnAuthorized 401"?

+4
source share
1 answer

The quickest and easiest solution to your problem can be implemented using the same underscore method invoketo abort all promises when the callback fails:

var cols = [kf.Collections.invoices, kf.Collections.quotes, kf.Collections.bankaccounts, kf.Collections.invoicepaymentmethods];

var colsComplete = _.invoke(cols, 'fetch');

$.when.apply(null, colsComplete).then(doneCallback, function () {
    _.invoke(colsComplete, 'abort'); 
});

promises. , , abort promises .

readyState - readyState.

+1

Source: https://habr.com/ru/post/1568263/


All Articles