I will keep it in my memory. Usually I will write a singleton module to process it.
auth.js:
class Auth {
getToken() {
if (this.token && !isExpired(this.token)) {
return Promise.resolve(this.token);
}
return asyncCallApiForToken();
}
}
module.exports = new Auth();
main.js
const auth = require('./auth.js)
auth.getToken()
.then(token => {
// we got token here
}
source
share