How to use the axios.CancelToken function in axios

To help you get started, weโ€™ve selected a few axios examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github vtex / node-vtex-api / src / HttpClient / middlewares / cancellationToken.ts View on Github external
const handleCancellation = (ctx: MiddlewareContext, cancellation: Cancellation) => {
  let cancellable = true
  return {
    cancelToken: new axios.CancelToken((canceller) => {
      cancellation.source.token.promise.then(cancel => {
        if (cancellable) {
          canceller(cancel.message)
        }
      })
    }),
    onRequestFinish: () => {
      if (!ctx.config.cancelToken) {
        // don't have cancelToken: not cancelable
        cancellable = false
      } else if (!ctx.response) {
        // response is not ready: cancelable
        cancellable = true
      } else if (ctx.config.responseType !== 'stream') {
        // response is ready and it is not a stream: not cancelable
        cancellable = false
github Tencent / bk-cmdb / src / ui / src / api / index.js View on Github external
function getCancelToken() {
  let cancelExcutor
  const cancelToken = new Axios.CancelToken((excutor) => {
    cancelExcutor = excutor
  })
  return {
    cancelToken,
    cancelExcutor
  }
}
github xlsdg / dva-antd-starter / src / utils / request.js View on Github external
function fetch(method, url, data, inConfig = false) {
  let cancel = () => {};
  const cancelToken = new Axios.CancelToken((c) => {
    cancel = c;
  });

  const promise = (inConfig
    ? Request[method](url, {
      params: data,
      cancelToken
    })
    : Request[method](url, data, {
      cancelToken
    })
  );

  return {
    promise,
    cancel
github ArthurYung / react-my-website / src / page / blog / comments.js View on Github external
async getArticleReactions(dataInfo) {
    this.getReactionsCancel()
    const { data } = await Axios({ 
      url: `${dataInfo.url}/reactions?time=${Date.now()}`,
      cancelToken: new CancelToken((c) => {
        this.getReactionsCancel = c;
      }),
      headers: {
        'Accept': 'application/vnd.github.squirrel-girl-preview+json'
      }
    })
    this.setState({ reactions: data || [] })
  }
  async getArticleComments(dataInfo) {
github shuiRong / Gakki / src / pages / FollowRequestList.js View on Github external
followRequests = () => {
    followRequests(mobx.domain, {
      cancelToken: new CancelToken(c => (this.cancel = c))
    })
      .then(res => {
        this.setState({
          list: this.state.list.concat(res),
          loading: false
        })
      })
      .catch(() => {
        this.setState({
          loading: false
        })
      })
  }
github Tencent / bk-bcs-saas / bcs-app / frontend / src / api / index.js View on Github external
function getCancelToken () {
    let cancelExcutor
    const cancelToken = new axios.CancelToken(excutor => {
        cancelExcutor = excutor
    })
    return {
        cancelToken,
        cancelExcutor
    }
}
github Tencent / bk-PaaS / paas-ce / lesscode / lib / client / src / api / index.js View on Github external
function getCancelToken () {
    let cancelExcutor
    const cancelToken = new axios.CancelToken(excutor => {
        cancelExcutor = excutor
    })
    return {
        cancelToken,
        cancelExcutor
    }
}
github fxpio / fxp-satis-serverless / src / ui / api / BaseService.ts View on Github external
protected async request(config: AxiosRequestConfig, canceler?: Canceler): Promise {
        if (canceler) {
            config.cancelToken = new axios.CancelToken(function executor(c) {
                canceler.setExecutor(c);
            });
        }

        try {
            let res = await this.axios.request(config);

            return res.data;
        } catch (e) {
            if (!axios.isCancel(e)) {
                throw e;
            }
        }

        return null;
    }