Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

接口请求失败重试机制 #5

Open
54leibo opened this issue Nov 6, 2019 · 0 comments
Open

接口请求失败重试机制 #5

54leibo opened this issue Nov 6, 2019 · 0 comments

Comments

@54leibo
Copy link
Owner

54leibo commented Nov 6, 2019

function _ajax() {
    return new Promise((resolve, reject) => {
    ...
    })
 }
/**
 * HTTP Error retry
 * 
 * @params {Object} options: ajax params
 * @params {Number | undefined} limitCount: retry max count
 * @params {Number | undefined} providedCount: current retry count
*/
  function _retry(options, limitCount, providedCount) {
    // set default value
    if(!limitCount) {
      limitCount = 3
    }
    if(!providedCount) {
      providedCount = 0
    }
    return _ajax(options)
      .catch(err => {
      // retry condition
        if(err.name === 'HTTPError' && providedCount < limitCount) {
          return new Promise(resolve => {
          // retry after 1 second
            setTimeout(() => {
              resolve(_retry(options, limitCount, providedCount++))
            }, 1000)
          })
        }
        throw err
      })
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant