CHAN.RUN
function tryAsync<TArgs, T, TErrors>(fn, ...args): Promise<AsyncResult<T, InferFaultErrors<TErrors>>>;Defined in: src/try.ts
Run async code, return a discriminated result — never rejects.
Pass a declared function directly to get typed errors:
const getUser = declares([NotFoundError], async (id: string) => fetchUser(id));
const result = await tryAsync(getUser, "123");
// result.error is NotFoundError, not unknownOr wrap any expression in a lambda:
const result = await tryAsync(() => fetchUser(id));
// result.error is unknown| Type Parameter |
|---|
TArgs extends unknown[] |
T |
TErrors extends NamedFaultErrorClass<string>[] |
| Parameter | Type |
|---|---|
fn | DeclaredFn<TArgs, Promise<T>, TErrors> |
...args | TArgs |
Promise<AsyncResult<T, InferFaultErrors<TErrors>>>
const result = await tryAsync(getUser, req.params.id);
if (!result.ok) {
return match(result.error, [NotFoundError, DbError], {
NotFoundError: (e) => res.status(404).json({ error: e.message }),
DbError: () => res.status(503).json({ error: "Service unavailable" }),
});
}
res.json(result.data);const result = await tryAsync(() => fetch("https://api.example.com/data"));
if (!result.ok) {
logger.error("API request failed", { error: result.error });
return fallbackData;
}
const data = await result.data.json();async function UserPage({ id }: { id: string }) {
const result = await tryAsync(getUser, id);
if (!result.ok) return <ErrorCard error={result.error} />;
return <UserProfile user={result.data} />;
}async function onSubmit(values: FormValues) {
const result = await tryAsync(() => api.post("/signup", values));
if (!result.ok) {
match(result.error, {
ValidationError: (e) => setFieldErrors(e.message),
_: () => toast.error("Something went wrong"),
});
return;
}
router.push("/dashboard");
}import { readFile } from "node:fs/promises";
const result = await tryAsync(() => readFile("./data.json", "utf-8"));
if (!result.ok) {
console.error("Could not read file:", result.error);
process.exit(1);
}
const config = JSON.parse(result.data);function tryAsync<T>(fn): Promise<AsyncResult<T>>;Defined in: src/try.ts
Run async code, return a discriminated result — never rejects.
Pass a declared function directly to get typed errors:
const getUser = declares([NotFoundError], async (id: string) => fetchUser(id));
const result = await tryAsync(getUser, "123");
// result.error is NotFoundError, not unknownOr wrap any expression in a lambda:
const result = await tryAsync(() => fetchUser(id));
// result.error is unknown| Type Parameter |
|---|
T |
| Parameter | Type |
|---|---|
fn | () => Promise<T> |
Promise<AsyncResult<T>>
const result = await tryAsync(getUser, req.params.id);
if (!result.ok) {
return match(result.error, [NotFoundError, DbError], {
NotFoundError: (e) => res.status(404).json({ error: e.message }),
DbError: () => res.status(503).json({ error: "Service unavailable" }),
});
}
res.json(result.data);const result = await tryAsync(() => fetch("https://api.example.com/data"));
if (!result.ok) {
logger.error("API request failed", { error: result.error });
return fallbackData;
}
const data = await result.data.json();async function UserPage({ id }: { id: string }) {
const result = await tryAsync(getUser, id);
if (!result.ok) return <ErrorCard error={result.error} />;
return <UserProfile user={result.data} />;
}async function onSubmit(values: FormValues) {
const result = await tryAsync(() => api.post("/signup", values));
if (!result.ok) {
match(result.error, {
ValidationError: (e) => setFieldErrors(e.message),
_: () => toast.error("Something went wrong"),
});
return;
}
router.push("/dashboard");
}import { readFile } from "node:fs/promises";
const result = await tryAsync(() => readFile("./data.json", "utf-8"));
if (!result.ok) {
console.error("Could not read file:", result.error);
process.exit(1);
}
const config = JSON.parse(result.data);