Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including abort-controller-timer with all npm packages installed. Try it out:

require("abort-controller/package.json"); // abort-controller is a peer dependency. var abortControllerTimer = require("abort-controller-timer")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

abort-controller-timer v1.0.6

An AbortController that aborts after a specified timeout

README.md

An AbortController that aborts after a specified timeout

install

yarn add abort-controller-timer abort-controller
yarn-tool add abort-controller-timer abort-controller
yt add abort-controller-timer abort-controller
import AbortControllerTimer from 'abort-controller-timer';

test(`base`, (done) =>
{
    expect.assertions(8);

    let start = Date.now();

    let timeout = 100;
    let actual = new AbortControllerTimer(timeout);

    actual.on('abort', () => {
        let d = Date.now() - start;
        console.dir(d);

        expect(d).toBeGreaterThanOrEqual(100);
        expect(d).toBeLessThanOrEqual(110);
    })

    setTimeout(() => {
        expect(actual.aborted).toBeFalsy();
        expect(actual.timer).toBeTruthy()
    }, 70);

    setTimeout(() => expect(actual.aborted).toBeTruthy(), 100);
    setTimeout(() => expect(actual.aborted).toBeTruthy(), 150);
    setTimeout(() =>
    {
        expect(actual.aborted).toBeTruthy();
        done();
    }, 200);

    expect(actual).toHaveProperty('ms', timeout);

});

test(`refresh`, (done) =>
{
    expect.assertions(12);

    let start = Date.now();

    let timeout = 100;
    let actual = new AbortControllerTimer(timeout);

    actual.on('abort', () => {
        let d = Date.now() - start;
        console.dir(d);

        expect(d).toBeGreaterThanOrEqual(170);
        expect(d).toBeLessThanOrEqual(180);
    })

    setTimeout(() => {
        expect(Date.now() - start).toBeGreaterThanOrEqual(70);
        expect(actual.aborted).toBeFalsy();
        expect(actual.refresh()).toBeTruthy()
    }, 70);

    setTimeout(() => {
        expect(Date.now() - start).toBeGreaterThanOrEqual(100);
        expect(actual.aborted).toBeFalsy()
    }, 100);
    setTimeout(() => {
        expect(Date.now() - start).toBeGreaterThanOrEqual(150);
        expect(actual.aborted).toBeFalsy()
    }, 150);
    setTimeout(() =>
    {
        expect(Date.now() - start).toBeGreaterThanOrEqual(200);
        expect(actual.aborted).toBeTruthy();
        done();
    }, 200);

    expect(actual).toHaveProperty('ms', timeout);

});
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free