Created
January 29, 2018 01:03
-
-
Save faceyspacey/ac2aff614ebbdbd51da70817bc0f0af4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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