How to properly reject a promise in async/await syntax in TypeScript?
Hello everyone! I started learning TypeScript and async/await, but I ran into a problem. The documentation says that an async function always returns a Promise. When I want to successfully resolve a promise, I just return a value. But how do I properly reject a promise inside an async function?
Here's an example that doesn't work as expected:
```typescript
async function fetchUserData(userId: string): Promise {
if (!userId) {
// I want to reject the promise with an error here
return new Error('User ID is required'); // This is wrong
}
const response = await fetch(`/api/users/${userId}`);
return response.json();
}
```
I tried using `throw new Error()`, but it seems strange to me because throw is usually for exceptions, not for rejecting promises.