Skip to content

Instantly share code, notes, and snippets.

@dcherman
Created May 18, 2015 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcherman/4dfba0d72c008ee5b59d to your computer and use it in GitHub Desktop.
Save dcherman/4dfba0d72c008ee5b59d to your computer and use it in GitHub Desktop.
function promiseReduce( entries, reducer, initialValue ) {
return new Promise(function( resolve, reject ) {
(function next( i, memo ) {
Promise.resolve( reducer( memo, entries[ i ], i, entries ) ).then(function( returnValue ) {
if ( i === entries.length - 1 ) {
resolve( returnValue );
} else {
next( i + 1, returnValue );
}
}).catch( reject )
}( 0, initialValue ));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment