fetch timeout sample

    @@ -11,8 +11,8 @@ // https://developer.mozilla.org/en-US/docs/Web/API/AbortController/AbortController // https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal -// check in Chromium 66.0.3359.139 -// TODO Firefox did not working? +// Chromium | 66.0.3359.139 | 66.0.3359.139-0ubuntu0.16.04.3 +// Firefox | 60.0 20180509235617 | 60.0+build2-0ubuntu0.16.04.1 (async () => { 'use strict'; @@ -26,7 +26,7 @@ const text = await response.text(); console.log(text); } catch (e) { - console.erroe(e); + console.error(e); } finally { clearTimeout(timer); } @@ -34,4 +34,3 @@ // VM105:8 Uncaught (in promise) DOMException: The user aborted a request. })(); -
  • /*
     * @title fetch timeout demo
     * @description timeouted fetch w/ AbortController
     * @include http://*
     * @include https://*
     * @license CC0 Univ PD https://creativecommons.org/publicdomain/zero/1.0/
     */
    
    // Chrome/ium 66-
    // https://developer.mozilla.org/en-US/docs/Web/API/AbortController
    // https://developer.mozilla.org/en-US/docs/Web/API/AbortController/AbortController
    // https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
    
    // Chromium | 66.0.3359.139       | 66.0.3359.139-0ubuntu0.16.04.3
    // Firefox  | 60.0 20180509235617 | 60.0+build2-0ubuntu0.16.04.1
    (async () => {
        'use strict';
    
        const controller = new AbortController();
        const signal = controller.signal;
    
        const TIMEOUT = 10;
        const timer = setTimeout(() => controller.abort(), TIMEOUT);
        try {
            const response = await fetch(location.href, {signal,});
            const text = await response.text();
            console.log(text);
        } catch (e) {
            console.error(e);
        } finally {
            clearTimeout(timer);
        }
        // Promise {<pending>}
        // VM105:8 Uncaught (in promise) DOMException: The user aborted a request.
    })();
    
    
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2018/05/11 14:10:43 - 2018-05-11
  2. 2018/05/11 08:33:11 - 2018-05-11
  3. 2018/05/11 08:26:10 - 2018-05-11