Skip to content

Instantly share code, notes, and snippets.

@vinicius73
Created March 5, 2017 22:40
Show Gist options
  • Save vinicius73/98b47d0998cc7de6ea932ab83f5a2252 to your computer and use it in GitHub Desktop.
Save vinicius73/98b47d0998cc7de6ea932ab83f5a2252 to your computer and use it in GitHub Desktop.
tries
import { identity } from 'lodash'
import waith from './waith'
export default (action, repetitions, timeToWaith = 3000) => {
const onError = counter => () => {
if (counter < repetitions) {
return run(counter + 1)
}
return Promise.reject(new Error('TRIES_FAILURE'))
}
const run = counter => waith(timeToWaith)
.then(action)
.then(identity, onError(counter))
return run(1)
}
export default (time = 1000) => new Promise(resolve => setTimeout(resolve, time))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment