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 )
2
2
3
- My awesome module .
3
+ Create an AbortSignal that aborts after a delay .
4
4
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 )
6
6
7
7
## Install
8
8
9
9
``` sh
10
- npm install the-module
10
+ npm install timeout-signal
11
11
```
12
12
13
13
## Usage
14
14
15
15
``` 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
+ })
20
28
```
21
29
22
30
## API
23
31
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)
35
33
36
- ##### postfix
34
+ #### timeout
37
35
38
- Type: ` string ` \
39
- Default: ` rainbows `
36
+ Type: ` integer `
40
37
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
+
1
3
/**
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.
5
6
@example
6
7
```
7
- const theModule = require("the-module");
8
+ const timeoutSignal = require("timeout-signal");
9
+ const fetch = require("cross-fetch");
8
10
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
+ })
11
20
```
12
21
*/
13
- declare function theModule ( input : string , { postfix } : { postfix ?: string } ) : string
22
+ declare function timeoutSignal ( timeout : number ) : AbortSignal
14
23
15
- export = theModule
24
+ export = timeoutSignal
Original file line number Diff line number Diff line change 1
1
"use strict"
2
2
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 } ` )
6
8
}
7
9
8
- return `${ input } & ${ postfix } `
10
+ const controller = new AbortController ( )
11
+
12
+ setTimeout ( ( ) => {
13
+ controller . abort ( )
14
+ } , timeout )
15
+
16
+ return controller . signal
9
17
}
Original file line number Diff line number Diff line change 1
1
{
2
- "name" : " the-module " ,
2
+ "name" : " timeout-signal " ,
3
3
"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" ,
6
6
"author" :
" Richie Bendall <[email protected] >" ,
7
7
"license" : " MIT" ,
8
8
"main" : " index.js" ,
17
17
"lint" : " xo" ,
18
18
"test" : " xo && ava"
19
19
},
20
- "keywords" : [],
21
- "dependencies" : {},
20
+ "keywords" : [
21
+ " timeout" ,
22
+ " signal" ,
23
+ " abort"
24
+ ],
25
+ "dependencies" : {
26
+ "abort-controller" : " ^3.0.0"
27
+ },
22
28
"devDependencies" : {
23
29
"ava" : " ^3.8.1" ,
24
30
"eslint-config-richienb" : " ^0.4.2" ,
31
+ "p-event" : " ^4.1.0" ,
25
32
"xo" : " ^0.30.0"
26
33
},
27
34
"resolutions" : {
Original file line number Diff line number Diff line change 1
1
const test = require ( "ava" )
2
- const theModule = require ( "." )
2
+ const pEvent = require ( "p-event" )
3
+ const timeoutSignal = require ( "." )
3
4
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 )
11
7
12
- t . is ( theModule ( "unicorns" ) , "unicorns & rainbows" )
8
+ await pEvent ( signal , "abort" )
9
+
10
+ t . pass ( )
13
11
} )
You can’t perform that action at this time.
0 commit comments