Browse Source

return first result

master
silberengel 7 months ago
parent
commit
56a0dbb432
  1. 20
      src/lib/utils/websocket_utils.ts

20
src/lib/utils/websocket_utils.ts

@ -152,7 +152,7 @@ export async function fetchNostrEvent(filter: NostrFilter): Promise<NostrEvent |
} }
}); });
// Wait for the first successful result or all to fail with timeout // Wait for the first successful result with timeout
const timeoutPromise = new Promise<null>((resolve) => { const timeoutPromise = new Promise<null>((resolve) => {
setTimeout(() => { setTimeout(() => {
console.warn("[WebSocket Utils]: Fetch timeout reached"); console.warn("[WebSocket Utils]: Fetch timeout reached");
@ -160,18 +160,12 @@ export async function fetchNostrEvent(filter: NostrFilter): Promise<NostrEvent |
}, 5000); // 5 second timeout for the entire fetch operation }, 5000); // 5 second timeout for the entire fetch operation
}); });
const fetchPromise = Promise.allSettled(relayPromises).then((results) => { // Race between individual relay results and the timeout
// Find the first successful result const result = await Promise.race([
for (const result of results) { // Wait for the first successful result from any relay
if (result.status === 'fulfilled' && result.value) { Promise.race(relayPromises.filter(p => p !== null)),
return result.value; timeoutPromise
} ]);
}
return null;
});
// Race between the fetch and the timeout
const result = await Promise.race([fetchPromise, timeoutPromise]);
if (result) { if (result) {
return result; return result;

Loading…
Cancel
Save