I am trying to restructure a function more clearly and readable that has multiple return statements.
Usecase
Get a token:
- if the token does not exist in the database then generate a new one and return it.
- if the token exists -check if expired, if expired update it with a new one, -if not expired then return the same token.
Code
export const GetToken = async () => {
const db = new DB();
const check = await db.GetItem();
if(check.Count === 0) {
const token = await GenerateToken();
return token;
}
if(check.Count >= 1) {
const db_token = check.Items[0];
const isExpired = isTokenExpired(db_token);
if(isExpired) {
const token = await UpdateToken(db_token);
return token;
}
return db_token;
}
}
There are 3 return statements in this function and there is not a base return function.