TypeScript: pass rejected promise as a parameter

Viewed 131

I have the following function signature:

public myCustomFunction(): Promise<string> {
// Insert code here
}

I'm interested in returning a rejected promise with error. How can I create one? The promise should be already rejected, I'm not interested in creating a new promise with reject/success handlers.

1 Answers

You can create a promise in a pre-rejected state using Promise.reject:

The Promise.reject() method returns a Promise object that is rejected with a given reason.

As that quote suggests, you can pass a reason as an argument.

Related