Created
April 11, 2017 13:16
-
-
Save jmacqueen/23083f036922d98ba63e8583d89a8227 to your computer and use it in GitHub Desktop.
Promise with retry
This file contains hidden or 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 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