Callforth Promise Utilities

0.4.0 · active · verified Tue Apr 21

Callforth is a lightweight utility library designed to streamline common asynchronous JavaScript patterns by converting them into Promise-based operations. Currently at version 0.4.0, it offers a small collection of functions to replace traditional callback APIs, enabling developers to leverage `async/await` syntax for cleaner, more readable code. Its primary utilities include `eventOn` for awaiting specific events on any `EventTarget`, `timeout` for promise-based delays, and `polling` for repeatedly checking a predicate until it returns true or a maximum number of tries is reached. The library differentiates itself by focusing on simplifying frequently re-implemented patterns into a tiny, focused package, aiming to provide a 'don't callback, callforth' experience. There isn't a specified strict release cadence, but as a pre-1.0.0 project, updates are likely as needed.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates `eventOn` to asynchronously load a script and wait for its 'load' or 'error' event, combined with `timeout` for controlled delays, showcasing common browser-side async patterns.

import { eventOn, timeout } from "callforth";

async function loadAndTrackScript(url: string) {
  console.log(`Attempting to load script from: ${url}`);
  const script = document.createElement("script");
  script.src = url;
  script.async = true;

  // Append to document to start loading
  document.head.appendChild(script);

  try {
    // Await either 'load' for success or 'error' for failure
    const result = await eventOn(script, "load", "error");

    if (result.type === "load") {
      console.log(`Script loaded successfully from ${url} at ${new Date().toLocaleTimeString()}.`);
      await timeout(500); // Simulate a small delay after loading
      console.log("Further processing after a short pause.");
    } else if (result.type === "error") {
      console.error(`Failed to load script from ${url}.`);
    }
  } catch (err) {
    console.error(`An unexpected error occurred during script loading for ${url}:`, err);
  } finally {
    // Clean up the script element after it's done
    if (script.parentNode) {
      script.parentNode.removeChild(script);
    }
    console.log(`Cleanup for ${url} complete.`);
  }
}

// Example usage: Try loading a valid and an invalid script
loadAndTrackScript('https://unpkg.com/react@18/umd/react.production.min.js');
// In a real scenario, you'd replace 'nonexistent.js' with a URL that genuinely fails
// loadAndTrackScript('https://example.com/nonexistent.js');

view raw JSON →