Skip to content

Instantly share code, notes, and snippets.

@bas080
Created November 25, 2018 19:27
Show Gist options
  • Save bas080/4abb7c1523d1c7ccba3b81c93dabaa08 to your computer and use it in GitHub Desktop.
Save bas080/4abb7c1523d1c7ccba3b81c93dabaa08 to your computer and use it in GitHub Desktop.
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)
})
}
@bas080
Copy link
Author

bas080 commented Nov 26, 2018

Should reject with the last rejections when retries reaches 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment