layui 提取请求参数
JavaScript promises have always been a major win for the language -- they've led to a revolution of asynchronous coding that has vastly improved performance on the web. One shortcoming of native promises is that there's no true way to cancel a fetch
...until now. A new AbortController
has been added to the JavaScript specification that will allow developers to use a signal to abort one or multiple fetch calls.
JavaScript承诺一直是该语言的一大胜利-它们引发了异步编码的革命,极大地改善了Web性能。 原生承诺的一个缺点是,直到现在,还没有真正的方法来取消fetch
。 JavaScript规范中添加了一个新的AbortController
,它将允许开发人员使用信号来中止一个或多个提取调用。
Here's the flow of how canceling a fetch
call works:
以下是取消fetch
调用的工作流程:
Create an
AbortController
instance创建一个
AbortController
实例That instance has a
signal
property该实例具有
signal
属性Pass the
signal
as a fetch option forsignal
将
signal
作为signal
的获取选项signal
Call the
AbortController
'sabort
property to cancel all fetches that use that signal.调用
AbortController
的abort
属性来取消使用该信号的所有提取。
中止获取 (Aborting a Fetch)
The following is the bare bones of canceling a fetch request:
以下是取消获取请求的基本步骤:
const controller = new AbortController();
const { signal } = controller;
fetch("http://localhost:8000", { signal }).then(response => {
console.log(`Request 1 is complete!`);
}).catch(e => {
console.warn(`Fetch 1 error: ${e.message}`);
});
// Abort request
controller.abort();
An AbortError
occurs upon the abort
call, so you can listen for aborted fetches in the catch
by comparing the error name:
在abort
调用时发生AbortError
,因此您可以通过比较错误名称来监听catch
的中止提取:
}).catch(e => {
if(e.name === "AbortError") {
// We know it's been canceled!
}
});
Passing the same signal to multiple fetch
calls will cancel all requests with that signal:
将相同的信号传递给多个fetch
调用将取消该信号的所有请求:
const controller = new AbortController();
const { signal } = controller;
fetch("http://localhost:8000", { signal }).then(response => {
console.log(`Request 1 is complete!`);
}).catch(e => {
console.warn(`Fetch 1 error: ${e.message}`);
});
fetch("http://localhost:8000", { signal }).then(response => {
console.log(`Request 2 is complete!`);
}).catch(e => {
console.warn(`Fetch 2 error: ${e.message}`);
});
// Wait 2 seconds to abort both requests
setTimeout(() => controller.abort(), 2000);
In his article Abortable fetch, Jake Archibald details a nice utility for creating abortable fetches without the need for all of the boilerplate:
杰克·阿奇博尔德(Jake Archibald)在他的文章Abortable fetch中 ,详细介绍了一个不错的实用程序,它无需创建所有样板即可创建可中止的访存:
function abortableFetch(request, opts) {
const controller = new AbortController();
const signal = controller.signal;
return {
abort: () => controller.abort(),
ready: fetch(request, { ...opts, signal })
};
}
If I'm completely honest, I'm not super excited about the method for canceling fetches. In an ideal world, a basic .cancel()
on the Promise returned by a fetch would be cool, but there are issues that would come with that too. In any event, I'm jazzed about being able to cancel fetch
calls and you should be too!
如果我完全诚实,我对取消提取的方法并不感到兴奋。 在理想的世界中,通过获取返回的Promise的基本.cancel()
很酷,但是随之而来的还有一些问题。 无论如何,我很高兴能够取消fetch
呼叫,您也应该如此!
layui 提取请求参数