@await-of/of
v3.0.0
Published
Promise wrapper with some sugar
Maintainers
Readme
🍡🍭🍬 O F 🍡🍭🍬

💬 Usage
import { ofAnyCase } from "@await-of/of";
const promise = () => new Promise((resolve, _reject) => {
resolve({ data: true });
});
const config = {
defaults: "🤷 Default value in case of error",
error: new Error("💀 Custom error, replaces thrown error"),
retries: 3, // 🔁 Third time's a charm
timeout: 1_000, // ⏱️ Delay before timeout error
};
// no error thrown
const [result, error] = await ofAnyCase(promise(), config);
console.log(result); // { data: true }
console.warn(error); // no error thrown, so it's undefinedʕ◔ϖ◔ʔ Compare to GoLang style
import { of } from "@await-of/of";
interface DatabaseConnectionInterface extends AsyncDisposable {
isConnected: boolean;
version: string;
}
async function getDatabaseConnection(): Promise<DatabaseConnectionInterface> {
if (Math.random() > 0.5) {
return {
isConnected: true,
version: "18.3",
[Symbol.asyncDispose]: async (): Promise<void> => {
console.log("Closing connection…");
},
};
}
throw new Error("No connection available");
}
async function jsStyleErrorHandling(): Promise<void> {
try {
await using connection = await getDatabaseConnection();
console.log("Connection established successfully using JS-style.");
console.dir(connection);
} catch (error) {
console.warn("Something went wrong in our JS-style example");
console.warn(`${error}`);
return;
}
}
async function goStyleErrorHandling(): Promise<void> {
const [connection, error] = await of(getDatabaseConnection());
if (error) {
console.warn("Something went wrong in our GoLang-style example");
console.warn(`${error}`);
return;
}
console.log("Connection established successfully using GoLang-style.");
console.dir(connection);
}
await Promise.all([jsStyleErrorHandling(), goStyleErrorHandling()]);