How to properly reject a promise when using async/await in TypeScript?
Hello everyone! I recently started learning async/await in TypeScript and encountered a problem. I have a function that should perform an asynchronous operation, but under certain conditions I need to return an error (reject). The problem is that I don't understand how to properly "reject" a promise when using async/await syntax.
Here's an example of my code:
```typescript
async function fetchUserData(userId: string): Promise {
// Want to reject the promise if userId is empty
if (!userId) {
// How to properly reject here?
}
const response = await fetch(`/api/users/${userId}`);
return response.json();
}
```
I know that in classic Promise you can call reject(), but in an async function return automatically completes the promise with resolve. I tried throw
No answers yet.
Log in to leave an answer
Blogs
Generics in Swift: Why They Are Needed and How to Use Them
Functional Programming in Go: Advanced Techniques and Examples
Deploying a Kotlin Application on VPS: A Step-by-Step Guide from A to Z
How Swift Distinguishes a Simulator from a Real Device: A Complete Breakdown