Skip to content

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Controla

Control your function easily.

Npm Bundlephobia Coverage Typescript License Npm download

Install

npm install controla

API

controlAsyncFunction

import { controlAsyncFunction } from 'controla'

const outerAbortController = new AbortController()

const { run, abort } = controlAsyncFunction(
  async ({ signal }) => {
    return 'Hello, world!'
  },
  {
    signal: outerAbortController.signal, // Optional, AbortSignal
    timeout, // Optional, ms
  }
)

controlSingleFlight

Deduplicate concurrent async calls into a single shared "flight", with optional idle cache window and per-call controls.

import { controlSingleFlight } from 'controla'

// Your flight function (single-flight deduped); use AbortSignal to support cancellation
const fetchUser = async ({ signal }: { signal?: AbortSignal }) => {
  const res = await fetch('/api/user', { signal })
  if (!res.ok) throw new Error('request failed')
  return (await res.json()) as { id: string; name: string }
}

// Minimal usage
const { run, abortAll } = controlSingleFlight(fetchUser, { timeout: 10_000, idleReleaseTime: 1000 })
const u1 = await run().promise            // shared flight for concurrent callers
const u2 = await run().promise            // reused within idle window
await run({ refresh: true }).promise      // force a new flight
await run({ timeout: 500 }).promise       // per-call timeout
abortAll()                                // abort underlying flight for everyone

Key points:

  • run(options) returns { promise, abort }
  • options: signal?, timeout?, refresh?
  • Factory options: timeout, idleReleaseTime
  • Extra: abortAll() to cancel the active shared flight

About

Control your async function easily.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages