Concepts
Cleanup registry
Record owned cleanups, attempt them in reverse order once, and return failures for explicit reporting.
Formal Syntax & Structure
javascript syntaxjavascript
dispose() → cleanup failuresWorking Example
Example Snippetjavascript
(async () => {const seen=[],cleanups=[()=>seen.push("A"),()=>{seen.push("B");throw new Error("B");},()=>seen.push("C")];let disposed=false;function dispose(){if(disposed)return [];disposed=true;const errors=[];while(cleanups.length){try{cleanups.pop()();}catch(e){errors.push(e.message);}}return errors;}console.log(seen.length,JSON.stringify(dispose()),seen.join(","),JSON.stringify(dispose()));})()