Created
August 12, 2022 15:38
-
-
Save 0xDaedalus/84315230c45abab4c89c84ab61d457c9 to your computer and use it in GitHub Desktop.
Response
This file contains 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
type ValidResponse<T> = { | |
val: T | |
err: false | |
} | |
type InvalidResponse = { | |
val: unknown | |
err: true | |
} | |
type Response<T> = ValidResponse<T> | InvalidResponse | |
const getTheThing = async (): Response<AlchemyApiResponse> => { | |
try { | |
const res = await getResponseFromAlchemy() | |
return { | |
val: res, | |
err: false | |
} | |
} catch(e) { | |
return { | |
val: e, | |
err: true | |
} | |
} | |
} | |
const getTheThingAndDoAThing = async () => { | |
const { err, val: alchemyResponse } = await getTheThing() | |
if (err) { | |
// handle the error | |
} else { | |
doAThing(alchemyResponse) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment