constant
constant creates a CancellableHandle that always resolves to the given value. Useful for creating async functions that return fixed values.
Best for stubs and defaults Use
constantwhen you need a cancellable handle that resolves to a predetermined value.
Import
Section titled “Import”Root package:
import { constant } from "@vgerbot/async";Module subpath:
import { constant } from "@vgerbot/async/utils";Leaf subpath:
import { constant } from "@vgerbot/async/utils/constant";Quick example
Section titled “Quick example”import { constant } from "@vgerbot/async";
const handle = constant(42);const result = await handle.promise; // 42When to use constant
Section titled “When to use constant”- Stub functions: Create async stubs that return fixed values for testing.
- Default values: Provide a cancellable handle that resolves to a default.
- Pipeline placeholders: Insert a constant value into an async pipeline.
- Mock implementations: Replace real async functions with constant returns.
function constant<T>( value: T, options?: CancellableOptions<T>,): CancellableHandle<T>;Parameters
Section titled “Parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
value |
T |
— | The constant value to resolve with. |
options |
CancellableOptions<T> |
undefined |
Cancellable configuration options. |
Return value
Section titled “Return value”Returns a CancellableHandle<T> that resolves to value.
Execution model
Section titled “Execution model”constant wraps the value in a cancellable call. The value is returned immediately in the async function body.
const handle = constant({ id: 1, name: "default" });const config = await handle.promise; // { id: 1, name: "default" }Cancellation
Section titled “Cancellation”Since the value is returned immediately, cancellation has limited effect. However, the CancellableOptions are passed through, so timeout and signal still apply.
const controller = new AbortController();const handle = constant("result", { signal: controller.signal });
controller.abort(); // Cancels before the microtask runsTypeScript tips
Section titled “TypeScript tips”constant is generic over T, so the return type is inferred from the value.
const handle = constant("hello"); // CancellableHandle<string>const handle2 = constant(42); // CancellableHandle<number>const handle3 = constant(null); // CancellableHandle<null>Related APIs
Section titled “Related APIs”cancellable— the primitive used byconstant.asyncify— wraps sync functions.once— executes a function only once.noop— a no-op function.