Concepts

Network-first fallback policy (pattern)

Try the free JavaScript compiler

Editorial strategy: try the network and use an explicitly permitted fallback on transport failure. Not a built-in API.

Formal Syntax & Structure

javascript syntaxjavascript
await choose(network, cached)

Return Value

Promise<Response>

Editorial strategy: try the network and use an explicitly permitted fallback on transport failure. Not a built-in API.

Working Example

Example Snippetjavascript
(async () => {async function choose(network,cached){try{return await network();}catch{return cached??new Response("unavailable",{status:503});}}const r=await choose(()=>Promise.reject(new Error("simulated transport failure")),new Response("cached public data"));console.log(await r.text());})()
JavaScript Reference