File tree Expand file tree Collapse file tree 5 files changed +68
-49
lines changed Expand file tree Collapse file tree 5 files changed +68
-49
lines changed Original file line number Diff line number Diff line change 1- # the-module [ ![ Travis CI Build Status] ( https://img.shields.io/travis/com/Richienb/the-module /master.svg?style=for-the-badge )] ( https://travis-ci.com/Richienb/the-module )
1+ # timeout-signal [ ![ Travis CI Build Status] ( https://img.shields.io/travis/com/Richienb/timeout-signal /master.svg?style=for-the-badge )] ( https://travis-ci.com/Richienb/timeout-signal )
22
3- My awesome module .
3+ Create an AbortSignal that aborts after a delay .
44
5- [ ![ NPM Badge] ( https://nodei.co/npm/the-module .png )] ( https://npmjs.com/package/the-module )
5+ [ ![ NPM Badge] ( https://nodei.co/npm/timeout-signal .png )] ( https://npmjs.com/package/timeout-signal )
66
77## Install
88
99``` sh
10- npm install the-module
10+ npm install timeout-signal
1111```
1212
1313## Usage
1414
1515``` js
16- const theModule = require (" the-module" );
17-
18- theModule (" unicorns" );
19- // => 'unicorns & rainbows'
16+ const timeoutSignal = require (" timeout-signal" );
17+ const fetch = require (" cross-fetch" );
18+
19+ fetch (" https://www.google.com" , { signal: timeoutSignal (5000 ) })
20+ .then (response => {
21+ // Handle response
22+ })
23+ .catch (error => {
24+ if (error .message === " The user aborted a request." ) {
25+ // Handle abortion
26+ }
27+ })
2028```
2129
2230## API
2331
24- ### theModule(input, options?)
25-
26- #### input
27-
28- Type: ` string `
29-
30- Lorem ipsum.
31-
32- #### options
33-
34- Type: ` object `
32+ ### timeoutSignal(timeout)
3533
36- ##### postfix
34+ #### timeout
3735
38- Type: ` string ` \
39- Default: ` rainbows `
36+ Type: ` integer `
4037
41- Lorem ipsum .
38+ The milliseconds to wait .
Original file line number Diff line number Diff line change 1+ import { AbortSignal } from "abort-controller"
2+
13/**
2- My awesome module.
3- @param input Lorem ipsum.
4- @param postfix Lorem ipsum.
4+ Create an AbortSignal that aborts after a delay.
5+ @param timeout The milliseconds to wait.
56@example
67```
7- const theModule = require("the-module");
8+ const timeoutSignal = require("timeout-signal");
9+ const fetch = require("cross-fetch");
810
9- theModule("unicorns");
10- //=> 'unicorns & rainbows'
11+ fetch("https://www.google.com", { signal: timeoutSignal(5000) })
12+ .then(response => {
13+ // Handle response
14+ })
15+ .catch(error => {
16+ if (error.message === "The user aborted a request.") {
17+ // Handle abortion
18+ }
19+ })
1120```
1221*/
13- declare function theModule ( input : string , { postfix } : { postfix ?: string } ) : string
22+ declare function timeoutSignal ( timeout : number ) : AbortSignal
1423
15- export = theModule
24+ export = timeoutSignal
Original file line number Diff line number Diff line change 11"use strict"
22
3- module . exports = ( input , { postfix = "rainbows" } = { } ) => {
4- if ( typeof input !== "string" ) {
5- throw new TypeError ( `Expected a string, got ${ typeof input } ` )
3+ const { AbortController } = require ( "abort-controller" )
4+
5+ module . exports = timeout => {
6+ if ( ! Number . isInteger ( timeout ) ) {
7+ throw new TypeError ( `Expected an integer, got ${ typeof timeout } ` )
68 }
79
8- return `${ input } & ${ postfix } `
10+ const controller = new AbortController ( )
11+
12+ setTimeout ( ( ) => {
13+ controller . abort ( )
14+ } , timeout )
15+
16+ return controller . signal
917}
Original file line number Diff line number Diff line change 11{
2- "name" : " the-module " ,
2+ "name" : " timeout-signal " ,
33 "version" : " 0.0.0" ,
4- "description" : " My awesome module ." ,
5- "repository" : " https://github.com/Richienb/the-module .git" ,
4+ "description" : " Create an AbortSignal that aborts after a delay ." ,
5+ "repository" : " https://github.com/Richienb/timeout-signal .git" ,
66 "author" : " Richie Bendall <richiebendall@gmail.com>" ,
77 "license" : " MIT" ,
88 "main" : " index.js" ,
1717 "lint" : " xo" ,
1818 "test" : " xo && ava"
1919 },
20- "keywords" : [],
21- "dependencies" : {},
20+ "keywords" : [
21+ " timeout" ,
22+ " signal" ,
23+ " abort"
24+ ],
25+ "dependencies" : {
26+ "abort-controller" : " ^3.0.0"
27+ },
2228 "devDependencies" : {
2329 "ava" : " ^3.8.1" ,
2430 "eslint-config-richienb" : " ^0.4.2" ,
31+ "p-event" : " ^4.1.0" ,
2532 "xo" : " ^0.30.0"
2633 },
2734 "resolutions" : {
Original file line number Diff line number Diff line change 11const test = require ( "ava" )
2- const theModule = require ( "." )
2+ const pEvent = require ( "p-event" )
3+ const timeoutSignal = require ( "." )
34
4- test ( "main" , t => {
5- t . throws ( ( ) => {
6- theModule ( 123 )
7- } , {
8- instanceOf : TypeError ,
9- message : "Expected a string, got number"
10- } )
5+ test ( "main" , async t => {
6+ const signal = timeoutSignal ( 0 )
117
12- t . is ( theModule ( "unicorns" ) , "unicorns & rainbows" )
8+ await pEvent ( signal , "abort" )
9+
10+ t . pass ( )
1311} )
You can’t perform that action at this time.
0 commit comments