0

I've been struggling to clear an Interval. It is inside a function that I call on a forEach statement for multiple objects.

The clearInterval is called inside a catch() for a promise. When it fails the console.log() works but not the clearInterval.

The code is as follows:

const myfunction = (object) => {
    console.log(`Starting Interval for ${object.name}...`)
    var timerLoop = setInterval( () => {
        console.log(`Interval for ${object.name}`)
        doSomething(object)
        .then((result) => {
            console.log(result)
            doTheThing(result)
        })
        .catch( (err) => {
            console.log('There was an error')
            clearInterval(timerLoop)
            console.error(err)
        })
    }, 1000)
}

According to this question and it's answer, I should be able to call clearInterval() from inside the actual interval, but I don't seem to get what is wrong with it.

What am I missing?

Jorge Cuevas
  • 755
  • 3
  • 10
  • 30

0 Answers0