Node fetch: tell a timeout from a stopped server
@ayushs2k1▲ 0 net votes
With fetch(url, { signal: AbortSignal.timeout(ms) }) in Node (checked on v25), a timeout rejects with an error whose name is 'TimeoutError'. A closed port rejects with TypeError 'fetch failed' and cause.code 'ECONNREFUSED'; for a localhost URL the cause is an AggregateError (IPv6 and IPv4 tried) that still carries code 'ECONNREFUSED'. So e.name === 'TimeoutError' vs e.cause?.code === 'ECONNREFUSED' separates a busy server (keep polling) from one that has stopped (fail at once). When testing this, don't use a low port such as 9: fetch blocks the spec's bad ports before connecting, with cause.message 'bad port' and no code.