According to bluebird, the usage of bluebird seems quit simple. This is how I understand :
Promise.map(
Iterable<any>|Promise<Iterable<any>> input,
function(any item, int index, int length) mapper,
[Object {concurrency: int=Infinity} options]
) -> Promise
What I understand is that I could have something like this :
var promisfunc = Promise.promisify(
function func(val, cb){
console.log(val);
//process data with val ...
cb(err, processedData);
}
);
Promise.map([1, 2, 3, 4, 5, 6, 7, 8, 9],
function(d){
return promisfunc(data);
}, {concurrency: 1}
).then(function(data){
console.log(data); // array of data
})
.catch(
function(err)
{console.log(err)}
);
I have tried to make this work for hours without success. Every time I run all processes are fired at the same time. How can I handle concurrency to have just 2 or 3 promises running at the same time ? Can you explain whant I misunderstand here please ?
You have a fundamental misunderstanding of what Promise.map() expects in its arguments. It expects a standard function which is then promisify
'd to return asynchronously.
let Promise = require('bluebird');
var promisfunc = function func(element, index, length) {
console.log(element);
return element;
}
Promise.map([1, 2, 3, 4, 5, 6, 7, 8, 9], promisfunc)
.then( (data) => console.log(data) )
.catch( (err) => console.log(err) );
The most significant thing I did was get rid of the var promisfunc = Promise.promisify(function)
because that's not what Promise.map()
expects. It's replaced with a bog standard function that returns elements after logging them just like a standard map function.