UNPKG

redo-promise

Version:

Retry a promise, when async function call.

74 lines (43 loc) 1.73 kB
# redo-promise > Retry a promise, when async function call. 异步 Promise 函数重试机制。 [![Build Status](https://travis-ci.org/hustcc/redo-promise.svg?branch=master)](https://travis-ci.org/hustcc/redo-promise) [![Coverage Status](https://coveralls.io/repos/github/hustcc/redo-promise/badge.svg?branch=master)](https://coveralls.io/github/hustcc/redo-promise) [![npm](https://img.shields.io/npm/v/redo-promise.svg)](https://www.npmjs.com/package/redo-promise) [![npm](https://img.shields.io/npm/dm/redo-promise.svg)](https://www.npmjs.com/package/redo-promise) # Install Install By: > npm i --save redo-promise Then import it by: ```js 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. ```js 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`. ```js 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 in `ms`, default `1000`. - `max`: max retry count, default `5`. # License ISC@[hustcc](https://github.com/hustcc).