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

怎么正确的调用isCancel并传参 #308

Open
leehomeok opened this issue Aug 9, 2022 · 1 comment
Open

怎么正确的调用isCancel并传参 #308

leehomeok opened this issue Aug 9, 2022 · 1 comment

Comments

@leehomeok
Copy link

leehomeok commented Aug 9, 2022

image

我在 request的拦截器给每个请求加上了cancelToken

function addPending(config) {
  const pendingKey = getPendingKey(config);
  config.cancelToken = config.cancelToken || new Reqs.CancelToken((cancel) => {
    if (!pendingMap.has(pendingKey)) {
      pendingMap.set(pendingKey, cancel);
    }
  });
}

async function errHandler(response: Response, options: RequestOptionsInit, resJson: any) {
  const res = resJson ? resJson : await response?.clone?.()?.json?.() || {}
  console.log('isCancel', Reqs.isCancel(options), options)
  if(Reqs.isCancel(options.cancelToken)){
    console.log('Request canceled', options.message);
    return
  }
}

在response的时候 调用isCancel方法判断, 但每次都返回的false, 查看源码后,发现isCancel方法是判断传入的对象是否有__CANCEL__这个属性, 且__CANCEL__ == true

如上截图,传参是否应该为 .cancelToken.promise

@leehomeok
Copy link
Author

t通过查阅文档,发现isCancel 方法,应该放在errorHandler 异常处理函数里面

const errorHandler = (error: { response:  Response , request}) => {
  const { response, request } = error
 if(Reqs.isCancel(error)) {}

}

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