If I was doing purely functional programming I could probably do an early return, but in this case I want to break out of an if-statement early, something like this:
const doSomeWork = (resp, json) => {
if (json && json[CPStorageKeys.CP_AUTH_TOKEN]) {
const tokenFromBody = json[CPStorageKeys.CP_AUTH_TOKEN];
if (typeof tokenFromBody !== 'string') {
break breakto; // break out of if statement to goto the label below
}
if (tokenFromBody && tokenFromBody.length < 80) {
console.error('Token length from response body is too short.');
} else if (tokenFromBody) {
if (tokenFromBody != tokenFromHeader) {
console.warn('token from header does not match token from body:', tokenFromHeader, tokenFromBody)
}
console.log('user auth token:', tokenFromBody);
cpStorage.set(CPStorageKeys.CP_AUTH_TOKEN, tokenFromBody);
}
}
breakto: {};
return [resp, json];
}
but I don't know how to break out of the if block early using labels, anyone know a good way to do this that won't make me hate my own code later?!
Aucun commentaire:
Enregistrer un commentaire