PageSourceSearch

https://barnum-circus.github.io/assets/js/fcec7d51.c3198883.js

js barnum-circus.github.io collected 2026-10-03 08:42:40 UTC 3,375 bytes, 1 lines download raw bytes

1"use strict";(globalThis.webpackChunkbarnum_docs=globalThis.webpackChunkbarnum_docs||[]).push([[5849],{8204(e,n,t){t.r(n),t.d(n,{assets:()=>o,contentTitle:()=>s,default:()=>h,frontMatter:()=>a,metadata:()=>i,toc:()=>l});var r=t(6106),c=t(9236);const a={},s="Racing",i={id:"patterns/racing",title:"Racing",description:"race runs multiple actions concurrently and returns the result of the first one to finish. All other in-flight actions are cancelled.",source:"@site/versioned_docs/version-0.3/patterns/racing.md",sourceDirName:"patterns",slug:"/patterns/racing",permalink:"/docs/0.3/patterns/racing",draft:!1,unlisted:!1,tags:[],version:"0.3",frontMatter:{},sidebar:"documentationSidebar",previous:{title:"Timeout",permalink:"/docs/0.3/patterns/timeout"},next:{title:"Context and Variables",permalink:"/docs/0.3/patterns/context-and-variables"}},o={},l=[{value:"Basic race",id:"basic-race",level:2},{value:"Race for redundant work",id:"race-for-redundant-work",level:2},{value:"How it works",id:"how-it-works",level:2}];function d(e){const n={a:"a",code:"code",h1:"h1",h2:"h2",p:"p",pre:"pre",...(0,c.R)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(n.h1,{id:"racing",children:"Racing"}),"\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"race"})," runs multiple actions concurrently and returns the result of the first one to finish. All other in-flight actions are cancelled."]}),"\n",(0,r.jsx)(n.h2,{id:"basic-race",children:"Basic race"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-ts",children:"race(\n  queryPrimaryDB,\n  queryReplicaDB,\n  queryCache,\n)\n"})}),"\n",(0,r.jsx)(n.p,{children:"All three actions receive the same input and start concurrently. The first to complete wins \u2014 its result becomes the output. The other two are torn down (their subprocesses are orphaned and their completions are silently dropped)."}),"\n",(0,r.jsx)(n.h2,{id:"race-for-redundant-work",children:"Race for redundant work"}),"\n",(0,r.jsx)(n.p,{children:"When multiple agents can produce the same result, race them:"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-ts",children:'race(\n  pipe(constant("gpt-4"), callModel),\n  pipe(constant("claude"), callModel),\n)\n'})}),"\n",(0,r.jsx)(n.h2,{id:"how-it-works",children:"How it works"}),"\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"race"})," compiles to ",(0,r.jsx)(n.code,{children:"All"})," inside a ",(0,r.jsx)(n.code,{children:"RestartHandle"}),". Each branch chains the action with a ",(0,r.jsx)(n.code,{children:"RestartPerform"}),". The first to complete fires the effect, tearing down the ",(0,r.jsx)(n.code,{children:"All"})," and all its siblings. See ",(0,r.jsx)(n.a,{href:"/docs/0.3/architecture/algebraic-effect-handlers",children:"algebraic effect handlers"})," for the compilation details."]})]})}function h(e={}){const{wrapper:n}={...(0,c.R)(),...e.components};return n?(0,r.jsx)(n,{...e,children:(0,r.jsx)(d,{...e})}):d(e)}},9236(e,n,t){t.d(n,{R:()=>s,x:()=>i});var r=t(7378);const c={},a=r.createContext(c);function s(e){const n=r.useContext(a);return r.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function i(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(c):e.components||c:s(e.components),r.createElement(a.Provider,{value:n},e.children)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.