How to use async/await with Array.map in TypeScript?
Hello everyone! I'm a beginner TypeScript developer, and I'm having a problem using async/await with the `Array.map` method. I'm writing a function that should process an array of URLs, fetch data from each one, and return an array of results.
Here's my TypeScript code:
```typescript
interface Post {
id: number;
title: string;
}
async function fetchPost(id: number): Promise {
const response = await fetch(`https://jsonplaceholder.typicode.com/posts/${id}`);
return response.json();
}
async function fetchAllPosts(ids: number[]): Promise {
return ids.map(async (id) => {
const post = await fetchPost(id);
return post;
});
}
const result = await fetchAllPosts([1, 2, 3]);
console.log(result); // What will I get?
```
When I
No answers yet.
Log in to leave an answer