# Promise

`import { Promise } from "destack:async";`

Value produced by asynchronous execution.

A promise never rejects in Destack: recoverable failure remains an explicit `Result` value,
and panic remains the Worker fault boundary.
Every observer receives the same copyable value.

```ds title="Promise"
export class Promise<T: Copy> {
    constructor(executor: ((T) => void) => void);
    then<U: Copy>(onFulfilled: (T) => Promise<U>): Promise<U>;
    then<U: Copy>(onFulfilled: (T) => U): Promise<U>;
    finally(onFinally: () => Promise<void>): Promise<T>;
    finally(onFinally: () => void): Promise<T>;
    static resolve<T: Copy>(value: Promise<T>): Promise<T>;
    static resolve<T: Copy>(value: T): Promise<T>;
    static all<T: Copy>(values: Iterable<T | Promise<T>>): Promise<T[]>;
    static race<T: Copy>(values: Iterable<T | Promise<T>>): Promise<T>;
    static withResolvers<T: Copy>(): PromiseResolvers<T>;
    static block<T: Copy>(promise: Promise<T>): T;
}
```

[language/library/src/async/promise.ds:15:307](https://github.com/destack-sh/destack/blob/main/language/library/src/async/promise.ds#L15-L307)

## Members

### `constructor(executor: ((T) => void) => void)`

Create a promise from an eager executor.

### `then<U: Copy>(onFulfilled: (T) => Promise<U>): Promise<U>`

Continue when the promise fulfills, adopting the returned promise.

### `then<U: Copy>(onFulfilled: (T) => U): Promise<U>`

Continue when the promise fulfills.

### `finally(onFinally: () => Promise<void>): Promise<T>`

Continue after the promise fulfills, adopting the returned promise.

### `finally(onFinally: () => void): Promise<T>`

Continue after the promise fulfills, keeping the value.

### `static resolve<T: Copy>(value: Promise<T>): Promise<T>`

Return an existing promise.

### `static resolve<T: Copy>(value: T): Promise<T>`

Create a fulfilled promise.

### `static all<T: Copy>(values: Iterable<T | Promise<T>>): Promise<T[]>`

Wait for all promises.

### `static race<T: Copy>(values: Iterable<T | Promise<T>>): Promise<T>`

Wait for the first fulfilled promise.

### `static withResolvers<T: Copy>(): PromiseResolvers<T>`

Create a promise with an exposed resolving function.

### `static block<T: Copy>(promise: Promise<T>): T`

Block the current fiber until a promise fulfills.
