🔓 A polyfill/implementation of `Promise.withResolvers()`.

Info

This is useful for creating a promise and resolving/rejecting it from outside.

Syntax

TypeScript
import { withResolvers } from '@opentf/std';

withResolvers<T>(): {
  promise: Promise<T>;
  resolve: (value: T | PromiseLike<T>) => void;
  reject: (reason?: unknown) => void;
}

Returns

An object containing a new promise and its resolve/reject functions.

Examples

TypeScript
const { promise, resolve, reject } = withResolvers<number>();

setTimeout(() => {
  resolve(42);
}, 100);

const result = await promise; //=> 42
Last updated on
Edit this page