:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
/**
* Sometimes you want to try catch a simple function in a single assignment and this function helps you do that
* So instead of:
*
* let result: T
* try {
* result = await fn()
* } catch (e) {
* result = e
* }
*
* you can do:
*
* const [error, result] = await tryCatch(fn)
*/
export async function tryCatch<T>(fn: () => Promise<T>): Promise<[Error | null, T | null]> {
try {
const result = await fn()
return [null, result]
} catch (e) {
return [e, null]
}
}
export function tryCatchSync<T>(fn: () => T): [Error | null, T | null] {
try {
const result = fn()
return [null, result]
} catch (e) {
return [e, null]
}
}