New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

poll

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

poll - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

27

dist/cjs/poll.js

@@ -7,4 +7,4 @@ "use strict";

* @param {() => any} fn The function to call.
* @param {Number} delay The delay (in milliseconds) to wait before calling the function again.
* @param {() => Boolean} shouldStopPolling A callback function indicating whether to stop polling.
* @param {number} delay The delay (in milliseconds) to wait before calling the function again.
* @param {() => boolean} shouldStopPolling A callback function indicating whether to stop polling.
*/

@@ -15,18 +15,11 @@ async function poll(fn, delay, shouldStopPolling = () => false) {

}
else if (delay < 0) {
throw new RangeError(`Expected “delay” to be a non-negative number, but it was “${delay}”.`);
}
try {
do {
await fn();
if (shouldStopPolling()) {
break;
}
await new Promise(resolve => setTimeout(resolve, delay));
} while (!shouldStopPolling());
}
catch (error) {
throw error;
}
delay = Math.max(0, delay);
do {
await fn();
if (shouldStopPolling()) {
break;
}
await new Promise(resolve => setTimeout(resolve, delay));
} while (!shouldStopPolling());
}
exports.default = poll;
{
"name": "poll",
"version": "1.0.0",
"version": "1.0.1",
"license": "MIT",

@@ -5,0 +5,0 @@ "description": "A simple poll function based on async, await, and an infinite loop",

@@ -17,3 +17,3 @@ # poll

- [Documentation](#documentation)
- [Usage](#usage)
- [Examples](#examples)

@@ -71,3 +71,3 @@

function fn() {
function fn() {
console.log('Hello, beautiful!');

@@ -115,3 +115,3 @@ };

- **function**: Required. A function to be called every `delay` milliseconds. No parameters are passed to `function` upon calling it.
- **delay**: Required. The delay (in milliseconds) to wait before calling the function again.
- **delay**: Required. The delay (in milliseconds) to wait before calling the function again. If `delay` is negative, zero will be used instead.
- **shouldStopPolling**: Optional. A function indicating whether to stop the polling process. The callback function is evaluated twice during one iteration of the internal loop:

@@ -128,1 +128,42 @@ - After the result of the call to `function` was successfully awaited. In other words, right before triggering a new delay period.

None.
## Examples
The `poll` function expects two parameters: A callback function and a delay. After calling `poll` with these parameters, the callback function will be called. After it’s done being executed, the `poll` function will wait for the specified `delay`. After the delay, the process starts from the beginning.
```js
const pollDelayInMinutes = 10;
async function getStatusUpdates() {
const response = await fetch('/status');
console.log(response);
}
poll(getStatusUpdates, pollDelayInMinutes * 60 * 1000);
```
Note that `poll` will not cause a second call to the callback function if the first call is still not finished. For example, it the endpoint `/status` does not respond and the server doesn’t time out the connection, `poll` will still be waiting for the callback function to fully resolve. It will not start the delay until the callback function is finished.
### Stop polling
You can pass a callback function to `poll` for its last parameter. Its evaluated before and after calls to the polled function. If it evaluates to `true`, the `poll` function’s loop will stop and the function returns.
In the following example, the `shouldStopPolling` callback function evaluates to `true` after the `setTimeout` function called its anonymous callback function which sets `stopPolling` to `true`. The next time `shouldStopPolling` is evaluated, it will cause `poll` to exit normally.
```js
const pollDelayInMinutes = 10;
const stopPolling = false;
const shouldStopPolling = () => stopPolling;
function fn() {
console.log('Hello, beautiful!');
}
setTimeout(() => {
stopPolling = true;
}, 1000);
poll(fn, 50, shouldStopPolling);
```

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc