Created
September 26, 2017 01:56
-
-
Save distractdiverge/a3e4a621e3d522c6c92fbacd991e6f26 to your computer and use it in GitHub Desktop.
Example jsforce Retry Expired Token
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
const Promise = require('bluebird'); | |
const retry = require('bluebird-retry'); | |
class AuthError extends Error { | |
constructor(message) { | |
super(message); | |
} | |
} | |
let count = 0; | |
function testFunction() { | |
console.log(`Called ${count} times`); | |
count++; | |
if (count == 1) { | |
return Promise.reject(new Error('Fail the first time')); | |
} else if(count == 2) { | |
return Promise.reject(new AuthError('Token Expired')); | |
} else { | |
return Promise.resolve({message:'succeeded the third time', token:'abc123'}); | |
} | |
} | |
function getToken() { | |
return retry(testFunction, {max_retries: 3, interval: 500, backoff: 2, predicate: (err) => !(err instanceof AuthError)}) | |
.then(result => { | |
console.log(result); | |
return result.token; | |
}) | |
.catch(AuthError, err => { | |
console.log('Auth Expired'); | |
count = 3; // simulate resetting the auth | |
return getToken(); // rerun get Token | |
}); | |
} | |
getToken() | |
.then(token => { | |
console.log(`Token: ${token}`); | |
}); |
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
{ | |
"name": "promises", | |
"version": "1.0.0", | |
"main": "index.js", | |
"scripts": { | |
"start": "node index.js", | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "Alex Lapinski", | |
"license": "MIT", | |
"description": "", | |
"dependencies": { | |
"bluebird": "^3.5.0", | |
"bluebird-retry": "^0.11.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment