layui 提取请求参数_如何取消提取请求

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 for signal

    signal作为signal的获取选项signal

  • Call the AbortController's abort property to cancel all fetches that use that signal.

    调用AbortControllerabort属性来取消使用该信号的所有提取。

中止获取 (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呼叫,您也应该如此!

翻译自: https://davidwalsh.name/cancel-fetch

layui 提取请求参数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值