promise-condition
Rejects a promise that fails a test condition
var condition = require('promise-condition');
var rejectAbove400 = condition(function (data) { return data.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){
});
Composition
As promise-condition
input and output is a function returning a promise, you can compose them easily with other similar 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 promiseCondition = require('promise-condition');
var retryTwice = promiseRetry({ retries: 2 });
var rejectAbove500 = promiseCondition(function (data) {
return data.status < 500;
});
function fetchData() {
return fetch('/data');
}
retryTwice(rejectAbove500(fetchData))().then(function(response){
}).catch(function(err){
});
See also
promise-condition
composes really well with the following promise helper: