使用中止语法,您将能够执行以下操作: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);})