Parallel data fetching with error handling and timeouts

TypeScript Hard async

Task «Parallel data fetching with error handling and timeouts»

Implement an asynchronous function `fetchWithTimeout` that takes an array of URLs (strings) and a timeout in milliseconds. The function should send HTTP requests to all URLs in parallel using `fetch`. If a request takes longer than the specified timeout, it should be aborted (via `AbortController`). The function returns an array of results in the same order as the input URLs. For each URL, the result is a string: a successful response (response text) or an error message in the format "Error: <error type>", where <error type> is "Timeout" if the timeout expired, or "Network" if a network error occurred. It is guaranteed that the URLs are valid, but the server may not respond or may respond slowly. Use `Promise.allSettled` to handle all results.

Input Format

First line — number N (number of URLs). Then N lines, each containing a URL. Last line — number T (timeout in ms).

Output Format

N lines, each containing the result of a query (response text or error message).

main.ts