Last active
January 2, 2023 10:37
-
-
Save arihantdaga/6f589f1450fee92c39bd4f0dd5c7edb3 to your computer and use it in GitHub Desktop.
Avoiding branching
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
// Implementation 1 | |
async function makeRequest({ | |
method = RequestMethod.GET, | |
path, | |
body = {}, | |
tokenStore, | |
isLoginRequest = false, | |
}) { | |
// We can avoid this branching by using implementation 2 | |
const url = isLoginRequest | |
? new URL(path, VM_LOGIN_HOST) | |
: new URL(path, tokenStore?.api_url); | |
await Requests.makeRequest({ method, url, body, headers }); | |
} | |
// From LognOperator call | |
makeRequest(...args, (isLoginRequest = True)); | |
// From everywhere else | |
makeRequest(...args); | |
// Implementaion 2 | |
// Instead of isLoginRequest - add param host. | |
async function makeRequest({ | |
method = RequestMethod.GET, | |
host, | |
path, | |
body = {}, | |
tokenStore, | |
}) { | |
await Requests.makeRequest({ method, url, body, headers }); | |
} | |
// From LoginOperator | |
makeRequest(...args, (host = VM_LOGIN_HOST), (path = Login_url)); | |
// From everywhere else, know what should be the host | |
makeRequest(...args, (host = tokenStore.host)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment