Concurrency
withResolvers

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

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

Syntax

import { withResolvers } from '@opentf/std';
 
withResolvers<T>(): {
  promise: Promise<T>;
  resolve: (value: T | PromiseLike<T>) => void;
  reject: (reason?: any) => void;
}

Returns

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

Examples

const { promise, resolve, reject } = withResolvers<number>();
 
setTimeout(() => {
  resolve(42);
}, 100);
 
const result = await promise; //=> 42

Related

Try