Keyur Sakaria Keyur Sakaria - 4 months ago 10x
Javascript Question

Looping with promise bluebird

Is there any way to loop the promise by number like, function(i){})

So the above code will loop 5 times

Right now promise expects an array

Tex Tex

One option:, 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, function(i){})

//Or write your own reusable range() function (ES6)
var myRange = i => Array.from(Array(i).keys()), function(i){})