为了账号安全,请及时绑定邮箱和手机立即绑定

提取API请求超时?

提取API请求超时?

白猪掌柜的 2019-11-14 10:46:23
我有一个fetch-api POST要求:   fetch(url, {      method: 'POST',      body: formData,      credentials: 'include'    })我想知道默认的超时时间是多少?以及如何将其设置为3秒或不定秒的特定值?
查看完整描述

3 回答

?
慕后森

TA贡献1802条经验 获得超5个赞

使用中止语法,您将能够执行以下操作:


const controller = new AbortController();

const signal = controller.signal;


const fetchPromise = fetch(url, {signal});


// 5 second timeout:

const timeoutId = setTimeout(() => controller.abort(), 5000);



fetchPromise.then(response => {

  // completed request before timeout fired


  // If you only wanted to timeout the request, not the response, add:

  // clearTimeout(timeoutId);

})


查看完整回答
反对 回复 2019-11-14
  • 3 回答
  • 0 关注
  • 392 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信