Sunny-117/js-challenges

实现网络请求超时判断,超过三秒视为超时

Sunny-117 opened this issue · 3 comments

实现网络请求超时判断,超过三秒视为超时
function loadImg(src){
    new Promise((resolve,reject)=>{
        let img = new Image()
        img.onload=function(){
            console.log("图片加载成功")
            resolve(img)
        }
        img.error=function(){
            reject(new Error(`Can not load ${src}`))
        }
        img.src=src
        
    })
}
function timeout(){
    return new Promise((reject)=>{
        setTimeout(()=>{
            reject("超时")
        },3000)
    })
}
//判断图片加载是否超时
Promise.race([loadImg,timeout])
.then((data)=>{
    console.log(data)
}).catch(err=>{
    console.log(err)
})
// promise.race() 只要有一个promise 出错就报错
      function delay(time) {
        return new Promise((res, rej) => {
          setTimeout(() => {
            rej("超时");
          }, time * 1000);
        });
      }
      Promise.race([delay(3), requestData(url, "get")])
        .then((res) => {
          console.log("cg", res);
        })
        .catch((err) => {
          console.log("sb", err);
        });
/**
 * 请求超过4s自动失败
 * @returns {Promise<Awaited<unknown>>}
 */
function getData () {
  return Promise.race([
    new Promise((resolve, reject) => {
      // 模拟一个6s的请求
      setTimeout(() => {
        resolve('请求成功')
      }, 6000)
    }),
    new Promise((resolve, reject) => {
      setTimeout(() => {
        reject('请求失败')
      }, 4000)
    })
  ]).then(res => {
    console.log(res)
  }, (error) => {
    console.log(error)
  })
}