Question: I am having a strange memory leak when I use setTimeout.

I call the following code every 15 sec with an async function that returns an array of promises (Promise.all), and it is supposed to run again 15 sec after all the promises have resolved and it finished all the logic.

```
const schedule = (fn, seconds, runNow = false) => {
  Meteor.setTimeout(() => {
    try {          
      Promise.resolve(fn()).then(()=>{
        schedule(fn, seconds, false)
      }).catch((err)=>{
        log.error(err)
        schedule(fn, seconds, false)
      })
    } catch (e) {
      log.error(e);
    }
  }, runNow ? 0 : seconds * 1000);
};

```

I have tried both setTimeout and the above Meteor.setTimeout and they act the same, after an hour or so you can see a clear memory leak.

It is worth mentioning that even if I disable my code and the called function just returns, it still leaks.

Server: OSX, Node 9.3, Meteor 1.6

Found few other people having mem leaks with setTimeout but different scenarios.

**Update**
The problem was that the schedule function was being called in another task I did not know about and that one had very heavy async code and missing await, so it was being rescheduled before it finishes and after a while it adds up. Thanks for all the answers.

Answer: 
What happens if you get rid of the try catch?

```
const schedule = (fn, seconds, runNow = false) => {
  Meteor.setTimeout(() => {
    Promise.resolve()
    .then(fn)
    .then(
      ()=>
        schedule(fn, seconds, false)
    )
    .catch((err)=>{
      log.error(err)
      schedule(fn, seconds, false)
    });
  }, runNow ? 0 : seconds * 1000);
};

```

I'm not sure why you have a try and catch, I assume the fn is a function that can throw an error and does not return a promise (if it did it would not throw and reject instead).

For functions that can throw used in promise chains you just have to put them **in** the promise chain. Promise.resolve(fn()) does not put it in the chain but Promise.resolve().then(fn) will.

```js
var fn = function(){throw("Not caught in promise")};

var fn = function(){throw("Not caught in promise")};
Promise.resolve().then(fn)
.catch(e=>console.log("============ is caught"));

Promise.resolve(fn())
.catch(e=>console.log("not caught"));
```
I am not sure if this is the solution but in the ReactJS FAQ is a topic called [Arrow Function in Render](https://reactjs.org/docs/faq-functions.html#arrow-function-in-render) which sais: 

> 
> Using an arrow function in render creates a new function each time the 
>  component renders, which may have performance implications (see below).
> 
> 
> 

Maybe this is the case here too?

> 
> It is worth mentioning that even if I disable my code and the called function just returns, it still leaks.
> 
> 
> 

PS: Maybe you can add the minimal code example that still leaks?