Reputation: 700
Is there any way to loop the promise by number like
Promise.map(5, function(i){})
So the above code will loop 5 times
Right now promise expects an array
Upvotes: 0
Views: 365
Reputation: 2766
One option: Promise.map(Array.from(Array(5).keys()), function(i){})
Basically, you're looking for the range()
method, or at least a way to emulate it. If the Promise implementation you're using doesn't offer a range()
method (and the most excellent bluebird Promise library doesn't), the code I provided above is a pretty concise way of emulating it.
Other options:
//If you're using lodash, underscore or any other library with a .range() method
Promise.map(_.range(5), function(i){})
//Or write your own reusable range() function
// ES6 arrow function syntax
var myRange = i => Array.from(Array(i).keys())
// or classic function syntax
var myRange = function (i) {return Array.from(Array(i).keys())}
Promise.map(myRange(5), function(i){})
Upvotes: 3