The .each function .each not change the value that is passed along the chain:
I have simplified your code since I assume:
var items = ['one','two'];
For your code:
Promise.each(items, function(element) { return element+'.'; //return Promise.resolve(element+'.'); }) .then(function(allItems) { console.dir(allItems); });
The result will be ['one','two'] , because these are the allowed values โโof the items array. The return value inside each does not affect the contents of the value passed to the then chain.
The .map function, on the other hand, will have this effect:
Promise.map(items, function(element) { return element+'.'; //return Promise.resolve(element+'.'); }) .then(function(allItems) { console.dir(allItems); });
Here, the return value will be used to create a new array, which will then be passed to then . Here the result will be ['one.','two.'] .
The two allItems appearing in your code are different objects.
EDIT For sequential iteration with a mapping, I would write a helper function as follows:
function mapSeries(things, fn) { var results = []; return Promise.each(things, function(value, index, length) { var ret = fn(value, index, length); results.push(ret); return ret; }).thenReturn(results).all(); }
Source: Implementing Promise.series
t.niese
source share