Skip to content

Instantly share code, notes, and snippets.

@jmacqueen
Created April 11, 2017 13:16
Show Gist options
  • Save jmacqueen/23083f036922d98ba63e8583d89a8227 to your computer and use it in GitHub Desktop.
Save jmacqueen/23083f036922d98ba63e8583d89a8227 to your computer and use it in GitHub Desktop.
Promise with retry
function tryWithRefresh(funcs, catchFunc){
const outerPromises = funcs.map( fn => typeof fn === 'function' ? fn() : fn )
return Promise.all(outerPromises)
.catch( (err) => {
if (err.status !== 401) { console.log(err); return; }
authService.refreshToken()
.then( () => {
console.log('refresh success')
const innerPromises = funcs.map( fn => typeof fn === 'function' ? fn() : fn )
Promise.all(innerPromises)
.then(() => console.log('post-retry'))
.catch( (err) => catchFunc(err) )
} )
.catch( (err) => catchFunc(err) )
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment