{"action":"created","issue":{"url":"https://api.github.com/repos/petkaantonov/bluebird/issues/1053","repository_url":"https://api.github.com/repos/petkaantonov/bluebird","labels_url":"https://api.github.com/repos/petkaantonov/bluebird/issues/1053/labels{/name}","comments_url":"https://api.github.com/repos/petkaantonov/bluebird/issues/1053/comments","events_url":"https://api.github.com/repos/petkaantonov/bluebird/issues/1053/events","html_url":"https://github.com/petkaantonov/bluebird/issues/1053","id":143173546,"number":1053,"title":"Feature request for Promise.loop()","user":{"login":"jfriend00","id":6621575,"avatar_url":"https://avatars.githubusercontent.com/u/6621575?v=3","gravatar_id":"","url":"https://api.github.com/users/jfriend00","html_url":"https://github.com/jfriend00","followers_url":"https://api.github.com/users/jfriend00/followers","following_url":"https://api.github.com/users/jfriend00/following{/other_user}","gists_url":"https://api.github.com/users/jfriend00/gists{/gist_id}","starred_url":"https://api.github.com/users/jfriend00/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jfriend00/subscriptions","organizations_url":"https://api.github.com/users/jfriend00/orgs","repos_url":"https://api.github.com/users/jfriend00/repos","events_url":"https://api.github.com/users/jfriend00/events{/privacy}","received_events_url":"https://api.github.com/users/jfriend00/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/petkaantonov/bluebird/labels/3.0.0","name":"3.0.0","color":"5319e7"},{"url":"https://api.github.com/repos/petkaantonov/bluebird/labels/enhancement","name":"enhancement","color":"84b6eb"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2016-03-24T07:39:26Z","updated_at":"2016-05-06T17:26:53Z","closed_at":null,"body":"This is a feature request for the ability to call some async operation N times in sequence.  The idea is like Bluebird's existing `Promise.reduce()`, but there is no source array to iterate over.  \r\n\r\nI see this requested pretty regularly on StackOverflow when people are trying to call an API with some sequence of requests where each request increases an index value.  There is no underlying iterable they are iterating, just a `for` loop with an index.\r\n\r\nI think of this as `Promise.loop()`, but it could certainly have any appropriate name.  Here's the general idea:\r\n\r\n```javascript\r\n    // Create a sequenced loop from startLoop to endLoop (inclusive)\r\n    // The iterator is called with whatever the previous promise returned and \r\n    //    must return a promise when it is done.\r\n    // The iterator is called like this: iterator(result, index); where:\r\n    //     result is the fulfilled value of the prior promise in the chain\r\n    //     index is the current loop index\r\n    Promise.loop = function(startLoop, endLoop, increment, iterator, initialVal) {\r\n        var p = Promise.resolve(initialVal);\r\n        for (var i = startLoop; i <= endLoop; i+= increment) {\r\n            (function(index) {\r\n                p = p.then(function(result) {\r\n                    return iterator(result, index);\r\n                });\r\n            })(i);\r\n        }\r\n        return p;\r\n    };\r\n```\r\n\r\nSample usage example here: https://jsfiddle.net/jfriend00/rodrz6o3/"},"comment":{"url":"https://api.github.com/repos/petkaantonov/bluebird/issues/comments/217506490","html_url":"https://github.com/petkaantonov/bluebird/issues/1053#issuecomment-217506490","issue_url":"https://api.github.com/repos/petkaantonov/bluebird/issues/1053","id":217506490,"user":{"login":"petkaantonov","id":829734,"avatar_url":"https://avatars.githubusercontent.com/u/829734?v=3","gravatar_id":"","url":"https://api.github.com/users/petkaantonov","html_url":"https://github.com/petkaantonov","followers_url":"https://api.github.com/users/petkaantonov/followers","following_url":"https://api.github.com/users/petkaantonov/following{/other_user}","gists_url":"https://api.github.com/users/petkaantonov/gists{/gist_id}","starred_url":"https://api.github.com/users/petkaantonov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/petkaantonov/subscriptions","organizations_url":"https://api.github.com/users/petkaantonov/orgs","repos_url":"https://api.github.com/users/petkaantonov/repos","events_url":"https://api.github.com/users/petkaantonov/events{/privacy}","received_events_url":"https://api.github.com/users/petkaantonov/received_events","type":"User","site_admin":false},"created_at":"2016-05-06T17:26:53Z","updated_at":"2016-05-06T17:26:53Z","body":"I'm sort of +0, this gets asked pretty often, but the ease at which you can do loops simply using recursion is why I'm not entirely +1."}}