Giter Site home page Giter Site logo

fetch-timeout.js's Introduction

fetch-timeout.js

一个真正支持timeout的fetch,超时直接把请求cancel

详细了解fetch 用法请参考 MDN https://developer.mozilla.org/zh-CN/docs/Web/API/Fetch_API/Using_Fetch

fetch-timeout基于浏览器原生fetch进行包装。主要修改如下2点

1.增加timeout超时参数

fetchTimeout(url, {
    timeout: 5000
})

2.增加dataType数据响应类型,舍弃原生fetch处理返回数据格式操作,fetch-timeout内部通过dataType参数自动处理

fetchTimeout(url, {
    dataType: 'json'
})
// .then(response => response.json())   // 不需要这个处理
.then(data => {
    
})

3.原生fetch在http状态码404或者500的情况下仍然会resolve,fetch-timeout做了小的变更,错误状态码或者请求超时,统一走catch

GET 请求

fetchTimeout(url, {
    timeout: 5000
})
.then(data => {
    // 请求成功
})
.catch(error => {
    // 请求异常或超时
})

POST 请求

fetchTimeout(url, {
    method: 'POST',
    body: '',
    dataType: 'json',
    timeout: 5000
})
.then(htmlStr => {
    // 请求成功
})
.catch(error => {
    // 请求异常或超时
})

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.