天天看点

取消fetch请求

原理:

结合AbortController构造器,实现请求取消操作。
const abortController = new AbortController();
const { signal } = abortController;
fetch('http://localhost:8999/api/list', { signal })
	.then(res => res.json()).then(res => {
		// 正常业务操作
		
	}).catch(e => {
		if (e.name === 'AbortError'){
			// 取消请求
		}
	});
setTimeOut(() => {
	abortController.abort();
}, 500);
           

继续阅读