Skip to content

Instantly share code, notes, and snippets.

@arihantdaga
Last active January 2, 2023 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arihantdaga/6f589f1450fee92c39bd4f0dd5c7edb3 to your computer and use it in GitHub Desktop.
Save arihantdaga/6f589f1450fee92c39bd4f0dd5c7edb3 to your computer and use it in GitHub Desktop.
Avoiding branching
// 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