pleroma-fe/src/services/promise_interval/promise_interval.js
Henry Jameson 9eee55df4a lint
2026-06-16 18:45:58 +03:00

37 lines
936 B
JavaScript

// promiseInterval - replacement for setInterval for promises, starts counting
// the interval only after a promise is done instead of immediately.
// - promiseCall is a function that returns a promise, it's called the first
// time after the first interval.
// - interval is the interval delay in ms.
const wait = (timeout) => {
let timeoutId
const promise = () =>
new Promise((resolve) => {
timeoutId = window.setTimeout(() => resolve(), timeout)
})
return { timeoutId, promise }
}
export const promiseInterval = (promiseCall, interval) => {
let stopped = false
let timeout = null
const stopFetcher = () => {
stopped = true
window.clearTimeout(timeout)
}
const loop = async () => {
while (!stopped) {
await promiseCall()
const { timeoutId, promise } = wait(interval)
timeout = timeoutId
await promise()
}
}
loop().then()
return { stop: stopFetcher }
}