Created
November 25, 2018 19:27
-
-
Save bas080/4abb7c1523d1c7ccba3b81c93dabaa08 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
function rejections(promises) { | |
let failed = [] | |
return Promise.all(promises.map(promise => | |
promise.catch(err => { | |
failed = [err, ...failed] | |
return err | |
})) | |
) | |
.then(() => failed) | |
} | |
module.exports = function migrate(functions, retries) { | |
if (retries == null) | |
return migrate(functions, functions.length * 2) | |
if (retries === 0) | |
return Promise.resolve() | |
return rejections(functions.map(fn => fn(functions, retries).catch(() => Promise.reject(fn)))) | |
.then(rejected => { | |
if (rejected.length === 0) | |
return Promise.resolve({retries}) | |
return migrate(rejected, retries - 1) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should reject with the last rejections when retries reaches 0.