question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Promise.reduce skips first array element.

See original GitHub issue

http://jsfiddle.net/RichAyotte/t0rjhrab/

var pets = ['cat', 'dog', 'bird'];
Promise.reduce(pets, function(_, p) {
    console.log(p);   
});

Log

dog
bird

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
cantremembercommented, Feb 18, 2015

that is expected behavior. your 0th argument is being used as the initial value for the reduce

http://jsfiddle.net/cantremember/766up1an/

var pets = ['cat', 'dog', 'bird'];
Promise.reduce(pets, function(_, p) {
    console.log(p);
    return _ + ',' + p;
}).then(function(_) {
    console.log('=', _); // => 'cat,dog,bird'
});

whereas pass a 3rd argument, such as 'mouse' to Promise#reduce and you’ll see each element of your Array being visited

0reactions
cantremembercommented, Feb 18, 2015

thanks, good to know. i must have overlooked that

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to access zeroth element in reduce to count repeats in an ...
The reduce function works in a bit another way. It iterates over an array, aggregating items into a total value. The first argument...
Read more >
Array.prototype.reduce() - JavaScript - MDN Web Docs
The reducer walks through the array element-by-element, at each step adding the current array value to the result from the previous step (this ......
Read more >
One reduce() to rule them all — How to use reduce in JavaScript
Our first reducer — reducing an array to one value. Let's write a function which is going to sum together all numbers in...
Read more >
Why Using reduce() to Sequentially Resolve Promises Works
When we use it to sequentially resolve promises, the reduce() loop isn't actually slowing down at all. It's completely synchronous, doing its ...
Read more >
JavaScript Array reduce() Method - javatpoint
If we do not provide the initial value, the callback function will start its execution with index as 1, where it will skip...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found