npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

just-defer-call

v1.0.5

Published

The `deferCall` utility is a higher-order function that defers the execution of a given function until the returned function is called. It supports synchronous and asynchronous functions.

Downloads

144

Readme

Dynamic JSON Badge

// Use this
onClick={deferCall(fn, ⭐, 🔥, 🚀)}
// Instead of this
onClick={() => fn(😭, 😢,😞)}

deferCall Documentation

The deferCall utility is a higher-order function that defers the execution of a given function until the returned function is called. It supports synchronous and asynchronous functions.

Installation

yarn add just-defer-call
# or
npm i just-defer-call

Usage

Signature

export const deferCall =
  <T, Args extends unknown[]>(
    fn: (...args: Args) => T | Promise<T>,
    ...args: Args
  ): (() => T | Promise<T>) =>
    (): T | Promise<T> => {
      return fn(...args);
    };

Parameters

  • fn: The function to be deferred. It can be synchronous or asynchronous.
  • args: The arguments to pass to fn when the deferred function is executed.

Returns

A new function that, when called, invokes the original function fn with the provided arguments and returns the result of original fn.


Examples

Vanilla Example 1 - Simple deferred call

import { deferCall } from "just-defer-call";

function greet(name) {
  console.log(`Hello, ${name}!`);
}

const deferredGreet = deferCall(greet, 'Alice');

// Function execution is deferred
deferredGreet(); // Logs: "Hello, Alice!"

Vanilla Example 2 - pass as many arguments as you want

import { deferCall } from "just-defer-call";

function max(...args) {
  console.log(`Max is: ${Math.max.apply(null, args)}!`);
}

const deferredMax = deferCall(max, 1, 2, 5, 4, 3);

// Function execution is deferred
deferredMax(); // Logs: "Max is: 5!"

Vanilla Example 3 - onClick handler

import { deferCall } from "just-defer-call";

function onClickHandler(buttonIndex) {
  alert(`Pressed button index: ${buttonIndex}`);
}

document.querySelectorAll("button").forEach((element, index) => {
  element.addEventListener('click', deferCall(onClickHandler, index));
});

TypeScript Example

import { deferCall } from "just-defer-call";

function add(a: number, b: number): number {
  return a + b;
}

const deferredAdd = deferCall(add, 5, 10);

// Function execution is deferred
console.log(deferredAdd()); // Logs: 15

async function fetchData(url: string): Promise<string> {
  const response = await fetch(url);
  return response.text();
}

const deferredFetch = deferCall(fetchData, 'https://api.example.com');

// Deferred execution of async function
deferredFetch().then(data => console.log(data));

React Example

import React, { useState } from 'react';

import { deferCall } from "just-defer-call";

function App() {
  const [count, setCount] = useState(0);

  const increment = (value: number) => {
    setCount((prev) => prev + value);
  };

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={deferCall(increment, 1)}>Increment</button>
    </div>
  );
}

export default App;

Notes

  • If fn is asynchronous, the returned function also returns a promise. Make sure to handle it appropriately.
  • Useful for event handling, lazy evaluations, or deferring expensive computations.

Testing

Jest Test Case

function sum(a: number, b: number): number {
  return a + b;
}

async function stupidFunction(value: number): Promise<number> {
  void 1;
  return value;
}

describe("deferCall", () => {
  test("deferCall should defer a synchronous function", () => {
    const mockFn = jest.fn(sum);
    const deferred = deferCall(mockFn, 2, 3);

    expect(mockFn).not.toHaveBeenCalled();
    deferred();
    expect(mockFn).toHaveBeenCalledWith(2, 3);
  });

  test("deferCall should defer an asynchronous function", async () => {
    const mockFn = jest.fn(stupidFunction);
    const deferred = deferCall(mockFn, 42);

    expect(mockFn).not.toHaveBeenCalled();
    await deferred();
    expect(mockFn).toHaveBeenCalledWith(42);
  });

  test("use deferCall with real function", async () => {
    const value1 = Math.random();
    const value2 = Math.random();
    const valuesSum = value1 + value2;

    const deferred = deferCall(sum, value1, value2);

    expect(deferred()).toEqual(valuesSum);
  });

  test("use deferCall with real async function", async () => {
    const value = Math.random();

    const deferred = deferCall(stupidFunction, value);

    expect(await deferred()).toEqual(value);
  });
});