Skip to content

Commit 64b741c

Browse files
committedNov 30, 2023
docs: clean up readme
1 parent c5e9bc7 commit 64b741c

File tree

1 file changed

+66
-58
lines changed

1 file changed

+66
-58
lines changed
 

‎README.md

+66-58
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,28 @@
22

33
A simple poll function based on async, await, and an infinite loop.
44

5-
**Features**:
6-
7-
- Asynchronous callback function
8-
- Delay function to customize the polling interval (e.g. to implement exponential backoff)
9-
- Cancellation function to stop polling altogether (e.g. stop polling after 10 cycles or once a certain condition is fulfilled)
10-
115
**Links**:
126

137
- [**npmjs.com**/package/poll](https://www.npmjs.com/package/poll)
148
- [on BundlePhobia](https://bundlephobia.com/result?p=poll)
159
- [**github.com**/kleinfreund/poll](https://github.com/kleinfreund/poll)
1610

11+
**Features**:
12+
13+
- Customizable delay via callback function (e.g. to implement exponential backoff)
14+
- Stop polling programmatically (e.g. stop polling once a certain condition is fulfilled)
15+
1716
## Contents
1817

1918
- [Installation & usage](#installation-&-usage)
2019
- [As npm package](#as-npm-package)
2120
- [As plain JS file](#as-plain-js-file)
2221
- [Documentation](#documentation)
23-
- [Syntax](#syntax)
22+
- [Parameters](#parameters)
23+
- [`fn`](#fn)
24+
- [`delayOrDelayCallback`](#delayordelaycallback-optional)
25+
- [`shouldStopPolling`](#shouldstoppolling-optional)
26+
- [Return value](#return-value)
2427
- [Examples](#examples)
2528
- [Minimal](#minimal)
2629
- [Stop polling](#stop-polling)
@@ -33,80 +36,86 @@ A simple poll function based on async, await, and an infinite loop.
3336

3437
### As npm package
3538

36-
1. Install the `poll` package.
39+
Install the `poll` package.
3740

38-
```sh
39-
npm install poll
40-
```
41+
```sh
42+
npm install poll
43+
```
44+
45+
Import the `poll` function and use it.
46+
47+
```js
48+
import { poll } from 'poll'
49+
50+
function fn() {
51+
console.log('Hello, beautiful!')
52+
}
53+
54+
poll(fn, 1000)
55+
```
56+
57+
### As plain JS file
58+
59+
Download the `poll` module.
4160

42-
2. Import the `poll` function and use it.
61+
```sh
62+
curl -O 'https://cdn.jsdelivr.net/npm/poll@latest/dist/poll.js'
63+
```
4364

44-
```js
45-
// “poll” is mapped to “poll/dist/poll.js” by Node.js via the package’s “exports” field.
46-
import { poll } from 'poll'
65+
Import the `poll` function and use it.
66+
67+
```html
68+
<script type="module">
69+
import { poll } from './poll.js'
4770
4871
function fn() {
4972
console.log('Hello, beautiful!')
5073
}
5174
5275
poll(fn, 1000)
53-
```
76+
</script>
77+
```
5478

55-
### As plain JS file
79+
## Documentation
5680

57-
1. Download the `poll` module.
81+
Basic usage of `poll` looks like this:
5882

59-
```sh
60-
curl -O 'https://cdn.jsdelivr.net/npm/poll@latest/dist/poll.js'
61-
```
83+
```ts
84+
poll(function () {
85+
console.log('Hello, beautiful!')
86+
}, 1000)
87+
```
6288

63-
2. Import the `poll` function and use it.
89+
### Parameters
6490

65-
```html
66-
<script type="module">
67-
import { poll } from './poll.js'
91+
#### `fn`
6892

69-
function fn() {
70-
console.log('Hello, beautiful!')
71-
}
93+
**Type**: `() => any`
7294

73-
poll(fn, 1000)
74-
</script>
75-
```
95+
A function to be called every `delay` milliseconds. No parameters are passed to `fn` upon calling it.
7696

77-
## Documentation
97+
#### `delayOrDelayCallback`
7898

79-
### Syntax
99+
**Type**: `number | (() => number)`
80100

81-
```
82-
poll(function, delay[, shouldStopPolling])
83-
```
101+
The delay (in milliseconds) to wait before calling the function `fn` again. If a function is provided instead of a number, it is evaluated during every polling cycle right before the wait period. If the delay is a negative number, zero will be used instead.
84102

85-
**Parameters**:
103+
#### `shouldStopPolling` (optional)
86104

87-
- **Name**: `fn`<br>
88-
**Type**: `() => any`<br>
89-
**Required**: Yes<br>
90-
**Description**: A function to be called every `delay` milliseconds. No parameters are passed to `fn` upon calling it.
91-
- **Name**: `delayOrDelayCallback`<br>
92-
**Type**: `number | (() => number)`<br>
93-
**Required**: Yes<br>
94-
**Description**: The delay (in milliseconds) to wait before calling the function `fn` again. If a function is provided instead of a number, it is evaluated during every polling cycle right before the wait period. If the delay is a negative number, zero will be used instead.
95-
- **Name**: `shouldStopPolling`<br>
96-
**Type**: `() => boolean | Promise<boolean>`<br>
97-
**Required**: No<br>
98-
**Default**: `() => false`<br>
99-
**Description**: A function (or a promise resolving to a function) indicating whether to stop the polling process by returning a truthy value (e.g. `true`). The `shouldStopPolling` callback function is called twice during one polling cycle:
105+
**Type**: `() => boolean | Promise<boolean>`<br>
106+
**Default**: `() => false`
100107

101-
- After the result of the call to `fn` was successfully awaited (right before triggering a new delay period).
102-
- After the `delay` has passed (right before calling `fn` again).
108+
A function returning a boolean (or a function returning a promise resolving to a boolean) indicating whether to stop the polling process. The `shouldStopPolling` callback function is called twice during one polling cycle:
103109

104-
This guarantees two things:
110+
- After the result of the call to `fn` was successfully awaited (right before triggering a new delay period).
111+
- After the `delay` has passed (right before calling `fn` again).
105112

106-
- A currently active execution of `fn` will be completed.
107-
- No new calls to `fn` will be triggered.
113+
This guarantees two things:
108114

109-
**Return value**:
115+
- A currently active execution of `fn` will be completed.
116+
- No new calls to `fn` will be triggered.
117+
118+
### Return value
110119

111120
None.
112121

@@ -137,7 +146,6 @@ You can pass a callback function to `poll` for its third parameter. It’s evalu
137146

138147
```js
139148
let stopPolling = false
140-
const shouldStopPolling = () => stopPolling
141149

142150
function fn() {
143151
console.log('Hello, beautiful!')
@@ -147,7 +155,7 @@ setTimeout(() => {
147155
stopPolling = true
148156
}, 1000)
149157

150-
poll(fn, 50, shouldStopPolling)
158+
poll(fn, 50, () => stopPolling)
151159
```
152160

153161
In this example, the `shouldStopPolling` callback function evaluates to `true` after the `setTimeout` function causes `stopPolling` to be set to `true` after 1000 milliseconds. The next time `shouldStopPolling` is evaluated, it will cause `poll` to exit normally.

0 commit comments

Comments
 (0)
Please sign in to comment.