logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://hacktivis.me/git/pleroma-fe.git

promise_interval.js (1007B)


  1. // promiseInterval - replacement for setInterval for promises, starts counting
  2. // the interval only after a promise is done instead of immediately.
  3. // - promiseCall is a function that returns a promise, it's called the first
  4. // time after the first interval.
  5. // - interval is the interval delay in ms.
  6. export const promiseInterval = (promiseCall, interval) => {
  7. let stopped = false
  8. let timeout = null
  9. const func = () => {
  10. const promise = promiseCall()
  11. // something unexpected happened and promiseCall did not
  12. // return a promise, abort the loop.
  13. if (!(promise && promise.finally)) {
  14. console.warn('promiseInterval: promise call did not return a promise, stopping interval.')
  15. return
  16. }
  17. promise.finally(() => {
  18. if (stopped) return
  19. timeout = window.setTimeout(func, interval)
  20. })
  21. }
  22. const stopFetcher = () => {
  23. stopped = true
  24. window.clearTimeout(timeout)
  25. }
  26. timeout = window.setTimeout(func, interval)
  27. return { stop: stopFetcher }
  28. }