
Product
Introducing Scala and Kotlin Support in Socket
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.
interval-promise
Advanced tools
setInterval with setTimeout semantics for promises and async/await
This library provides a simple mechanism for running a promise with a given amount of time between executions.
npm install interval-promise
Simple example using async-await
const interval = require('interval-promise')
// Run a function 10 times with 1 second between each iteration
interval(async () => {
await someOtherPromiseReturningFunction()
await another()
}, 1000, {iterations: 10})
interval(func, intervalLength, options = {}) // returns Promise<undefined>
Argument | Attritubes | Description |
---|---|---|
func | function | Required Function to execute for each interval. MUST return a promise. Two arguments are passed to this function.
|
intervalLength | number | function | Required Length in ms to wait between iterations. Should be (or return) a non-negative integer. If a function is used, one parameter iterationNumber (starting at 1) is passed. |
options | object | Optional settings (detailed below). |
options.iterations | number | Default: Infinity The number of times to execute the function. Must be Infinity or an integer greater than 0. |
options.stopOnError | boolean | Default: true If true, no subsequent calls will be made. The promise returned by interval() will be rejected and pass through the error thrown. |
This library was inspired by reissue.
There isn't currently direct feature to stop the iterations externally. You can, however, achieve this by checking a variable in the parent scope (of where the function is defined). Check out the code below.
const interval = require('interval-promise')
let stoppedExternally = false
const stopExternally = () => { stoppedExternally = true }
interval(async (iteration, stop) => {
if (stoppedExternally) {
stop()
}
// ... normal functionality ...
}, 1000)
// Some other work...
someOtherWork().then(() => {
// Now that our "other work" is done, we can stop our interval above with:
stopExternally()
})
FAQs
setInterval with setTimeout semantics for promises and async/await
The npm package interval-promise receives a total of 14,338 weekly downloads. As such, interval-promise popularity was classified as popular.
We found that interval-promise demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.
Application Security
/Security News
Socket CEO Feross Aboukhadijeh and a16z partner Joel de la Garza discuss vibe coding, AI-driven software development, and how the rise of LLMs, despite their risks, still points toward a more secure and innovative future.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.