Skip to content

Instantly share code, notes, and snippets.

@0xDaedalus
Created August 12, 2022 15:38
Show Gist options
  • Save 0xDaedalus/84315230c45abab4c89c84ab61d457c9 to your computer and use it in GitHub Desktop.
Save 0xDaedalus/84315230c45abab4c89c84ab61d457c9 to your computer and use it in GitHub Desktop.
Response
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