Skip to content

The catch method of Promise seems unreasonable #32

@uinz

Description

@uinz

The original Promise method will union the result of resolve with the result of catch

async function main() {
    const value = await Promise.resolve("1").catch(() => undefined);
    //     ^? const value: string | undefiend
}

But now the catch signature must return the same type as resolve.

interface Promise<T> {
  // ...
  catch(
    onrejected?: ((reason: unknown) => T | PromiseLike<T>) | null | undefined
  ): Promise<T>;
}

In my opinion await task().catch(() => defaultValue) is a good development experience

Similar to rust's unwrap_or

const value = await task().catch(() => defaultValue)

In addition, I think onrejected as optional will also bring some ambiguity.

The .catch() of the following code is actually useless

const value = awiat task().catch()

Therefore, I suggest modifying it as follows

interface Promise<T> {
  // ...
  catch<R>(
    onrejected: ((reason: unknown) => R | PromiseLike<R>)
  ): Promise<T | R>;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions