Tries executing tasks in sequence until one succeeds. Returns the result of the first successful task. If all tasks fail, throws the last error.
The result type.
Array of async tasks to try in order.
Optional
Cancellable configuration options.
A cancellable handle resolving to the first successful result.
const handle = tryEach([ async () => { throw new Error("fail 1"); }, async () => { throw new Error("fail 2"); }, async () => "success",]);const result = await handle; // "success" Copy
const handle = tryEach([ async () => { throw new Error("fail 1"); }, async () => { throw new Error("fail 2"); }, async () => "success",]);const result = await handle; // "success"
// Try multiple API endpointsconst handle = tryEach([ async (token) => fetch("https://api1.example.com"), async (token) => fetch("https://api2.example.com"), async (token) => fetch("https://api3.example.com"),]); Copy
// Try multiple API endpointsconst handle = tryEach([ async (token) => fetch("https://api1.example.com"), async (token) => fetch("https://api2.example.com"), async (token) => fetch("https://api3.example.com"),]);
Tries executing tasks in sequence until one succeeds. Returns the result of the first successful task. If all tasks fail, throws the last error.