Skip to content

Instantly share code, notes, and snippets.

@faceyspacey
Created January 29, 2018 01:03
Show Gist options
  • Save faceyspacey/ac2aff614ebbdbd51da70817bc0f0af4 to your computer and use it in GitHub Desktop.
Save faceyspacey/ac2aff614ebbdbd51da70817bc0f0af4 to your computer and use it in GitHub Desktop.
'use strict';
module.exports = (arr, reducer, initVal) => new Promise((resolve, reject) => {
let i = 0;
const next = total => {
const item = arr[i];
if (!item) {
resolve(total);
return;
}
Promise.all([total, item])
.then(value => {
next(reducer(value[0], value[1], i++));
})
.catch(reject);
};
next(initVal);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment