Skip to content

await-of/of

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

126 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🍑🍭🍬 O F 🍑🍭🍬

https://of.js.org/

Syntactic sugar for asynchronous functions, promises, generators and synchronous functions.

NPM Version NPM Downloads TypeScript Typings

OF

πŸ’¬ 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()]);

Sponsor this project

Packages

 
 
 

Contributors