0.0.1 • Published 6 years ago
redo-promise v0.0.1
redo-promise
Retry a promise, when async function call. 异步 Promise 函数重试机制。
Install
Install By:
npm i --save redo-promise
Then import it by:
import retry from 'redo-promise';
Usage
You can create retry
by the API provided by redo-promise
. It has 3 parameters:
promiseFn
: the target promise function.predicate
: function decided whether should be retry.opts
: retry strategy.
import retry from 'redo-promise';
const fetchUser = id => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ id: 'hustcc', name: 'hustcc' });
}, 10);
});
};
const fn = retry(fetchUser, r => r.success);
// if the request result is not success, it will retry 5 times.
fn('hustcc').then(user => {}).catch(e => {});
You can customize the retry opts
.
import retry from 'redo-promise';
const fn = retry(fetchUser, r => r.success, { delay: 3000, max: 3 });
// if the request result is not success, it will retry 3 times per 3 seconds.
fn('hustcc').then(user => {}).catch(e => {});
opts
contains:
delay
: retry delay time inms
, default1000
.max
: max retry count, default5
.
License
ISC@hustcc.
0.0.1
6 years ago