promise-reject-status-above
Rejects a promise returned by fetch()
if status above threshold
var rejectStatusAbove = require('promise-reject-status-above');
var rejectAbove400 = rejectStatusAbove({status: 400});
function fetch200(){
return fetch('/path/to/a/http-200-ok');
}
function fetch400(){
return fetch('/path/to/a/http-400-bad-request');
}
rejectAbove400(fetch200)()
.then(function(response){
}).catch(function(err){
});
rejectAbove400(fetch400)()
.then(function(response){
}).catch(function(err){
});
Options
status
: positive (>= 0) number. The returned promise will be rejected if the
response's status is equal or above this number.
Composition
As promise-reject-status-above
input and output is a function returning a promise, you can compose them easily with other simial helpers (see below).
In the example below, our /data
API is a bit janky and might return HTTP 500 errors. We'll retry them twice before giving up.
var promiseRetry = require('promise-retry');
var rejectStatusAbove = require('promise-reject-status-above');
var retryTwice = promiseRetry({ retries: 2 });
var rejectAbove500 = rejectStatusAbove({status: 500});
function fetchData() {
return fetch('/data');
}
retryTwice(rejectAbove500(fetchData))().then(function(response){
}).catch(function(err){
});
See also
promise-reject-status-above
composes really well with the following promise helper: