{"action":"created","issue":{"url":"https://api.github.com/repos/petkaantonov/bluebird/issues/502","labels_url":"https://api.github.com/repos/petkaantonov/bluebird/issues/502/labels{/name}","comments_url":"https://api.github.com/repos/petkaantonov/bluebird/issues/502/comments","events_url":"https://api.github.com/repos/petkaantonov/bluebird/issues/502/events","html_url":"https://github.com/petkaantonov/bluebird/issues/502","id":58184297,"number":502,"title":"Memory leak trying to create a while loop with promises","user":{"login":"colmaengus","id":6358852,"avatar_url":"https://avatars.githubusercontent.com/u/6358852?v=3","gravatar_id":"","url":"https://api.github.com/users/colmaengus","html_url":"https://github.com/colmaengus","followers_url":"https://api.github.com/users/colmaengus/followers","following_url":"https://api.github.com/users/colmaengus/following{/other_user}","gists_url":"https://api.github.com/users/colmaengus/gists{/gist_id}","starred_url":"https://api.github.com/users/colmaengus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/colmaengus/subscriptions","organizations_url":"https://api.github.com/users/colmaengus/orgs","repos_url":"https://api.github.com/users/colmaengus/repos","events_url":"https://api.github.com/users/colmaengus/events{/privacy}","received_events_url":"https://api.github.com/users/colmaengus/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-02-19T09:25:30Z","updated_at":"2015-02-19T12:48:30Z","closed_at":null,"body":"I'm trying to create a while loop using promises and I'm getting a memory leak.\r\nAn example of the pattern I'm using is \r\n\r\n```javascript\r\nvar promiseFor = BPromise.method(function(delay, condition, action, value) {\r\n  if (!condition(value)) {\r\n    return value;\r\n  } else {\r\n    return action(value).delay(delay).then(promiseFor.bind(null, delay, condition, action));\r\n  }\r\n});\r\n\r\nvar condition = function(value) {\r\n  return value != 100000;\r\n};\r\n\r\nvar action = function(value) {\r\n  return BPromise.delay(10)\r\n    .then(function() {\r\n      console.log('value:' + value);\r\n      return value + 1;\r\n    });\r\n};\r\n\r\nvar delay = 0;\r\n\r\npromiseFor(delay, condition, action, 0)\r\n  .then(function() {\r\n    console.log('done');\r\n  })\r\n  .catch(function(e) {\r\n    console.error(e.message);\r\n  });\r\n```\r\nWhen I run this the memory used by node keeps increasing.\r\nI'm using bluebird 2.9.10.\r\nI've tried another approach to avoid the recursive nature of the approach above but it also has a leak.\r\nAny suggestions on how to correctly implement a promise while loop ?"},"comment":{"url":"https://api.github.com/repos/petkaantonov/bluebird/issues/comments/75046450","html_url":"https://github.com/petkaantonov/bluebird/issues/502#issuecomment-75046450","issue_url":"https://api.github.com/repos/petkaantonov/bluebird/issues/502","id":75046450,"user":{"login":"colmaengus","id":6358852,"avatar_url":"https://avatars.githubusercontent.com/u/6358852?v=3","gravatar_id":"","url":"https://api.github.com/users/colmaengus","html_url":"https://github.com/colmaengus","followers_url":"https://api.github.com/users/colmaengus/followers","following_url":"https://api.github.com/users/colmaengus/following{/other_user}","gists_url":"https://api.github.com/users/colmaengus/gists{/gist_id}","starred_url":"https://api.github.com/users/colmaengus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/colmaengus/subscriptions","organizations_url":"https://api.github.com/users/colmaengus/orgs","repos_url":"https://api.github.com/users/colmaengus/repos","events_url":"https://api.github.com/users/colmaengus/events{/privacy}","received_events_url":"https://api.github.com/users/colmaengus/received_events","type":"User","site_admin":false},"created_at":"2015-02-19T12:48:29Z","updated_at":"2015-02-19T12:48:29Z","body":"@petkaantonov unfortunately this approach also has a leak. On windows the node process gained about 14 MB in 20 mins. I'm looking for some way to keep running a promise function over a long period of time without leaking memory."}}