UNPKG

@queso/attempt

Version:

Wraps func in a try...catch.

68 lines (49 loc) 4.66 kB
--- name: attempt menu: Util route: /attempt --- import { Link } from 'docz' import Editor from '../../src/components/Editor' # attempt [![npm](https://nodei.co/npm/@queso/attempt.svg?downloads=true)](https://nodei.co/npm/@queso/attempt/) <svg width="151.3" height="20" viewBox="0 0 1513 200" xmlns="http://www.w3.org/2000/svg"> <g> <rect fill="#555" width="864" height="200"/> <rect fill="#08C" x="864" width="649" height="200"/> </g> <g fill="#fff" textAnchor="start" fontFamily="Verdana,DejaVu Sans,sans-serif" fontSize="110"> <text x="220" y="148" textLength="604" fill="#000" opacity="0.1">min + gzip</text> <text x="210" y="138" textLength="604">min + gzip</text> <text x="919" y="148" textLength="549" fill="#000" opacity="0.1">126 bytes</text> <text x="909" y="138" textLength="549">126 bytes</text> </g> <image x="40" y="35" width="130" height="132" href="https://queso.surge.sh/public/favicon.png"/> </svg> [source](https://github.com/jedmao/queso/blob/master/src/packages/attempt/attempt.ts) Wraps `func` in a `try...catch`. ## Playground <Editor jsFunc="var%20attempt%20%3D%20%28function%20%28%29%20%7B%0A%09%27use%20strict%27%3B%0A%0A%09/**%0A%09%20*%20Wraps%20%60func%60%20in%20a%20%60try...catch%60.%0A%09%20*%20@param%20func%20The%20function%20to%20wrap.%0A%09%20*%20@category%20Util%0A%09%20*%20@returns%20A%20new%20wrapper%20function%20that%2C%20when%20called%2C%20returns%20a%20tuple%20of%20%60%5Berr%2C%20success%5D%60.%0A%09%20*%20@example%0A%09function%20exclaim%28message%3A%20string%29%20%7B%0A%09%20%20%20%20return%20message%20+%20%27%21%27%0A%09%7D%0A%09attempt%28exclaim%29%28%27hi%27%29%0A%09%20*/%0A%09function%20attempt%28%0A%09%09/**%0A%09%09%20*%20The%20function%20to%20wrap%20in%20a%20try...catch.%0A%09%09%20*/%0A%09%09func%2C%0A%09%29%20%7B%0A%09%09const%20wrapperName%20%3D%20%60attempt%28%24%7Bfunc.name%7D%29%60%3B%0A%09%09return%20%7B%0A%09%09%09/**%0A%09%09%09%20*%20This%20is%20a%20wrapper%20function%20provided%20by%20%60@queso/attempt%60.%0A%09%09%09%20*%20The%20original%20function%20is%20called%20inside%20of%20a%20try...catch.%0A%09%09%09%20*/%0A%09%09%09%5BwrapperName%5D%3A%20%28...args%29%20%3D%3E%20%7B%0A%09%09%09%09try%20%7B%0A%09%09%09%09%09return%20%5Bnull%2C%20func%28...args%29%5D%0A%09%09%09%09%7D%20catch%20%28reason%29%20%7B%0A%09%09%09%09%09return%20%5Breason%5D%0A%09%09%09%09%7D%0A%09%09%09%7D%2C%0A%09%09%7D%5BwrapperName%5D%0A%09%7D%0A%0A%09return%20attempt%3B%0A%0A%7D%28%29%29%3B%0A" tsFunc="/**%0A%20*%20Wraps%20%60func%60%20in%20a%20%60try...catch%60.%0A%20*%20@param%20func%20The%20function%20to%20wrap.%0A%20*%20@category%20Util%0A%20*%20@returns%20A%20new%20wrapper%20function%20that%2C%20when%20called%2C%20returns%20a%20tuple%20of%20%60%5Berr%2C%20success%5D%60.%0A%20*%20@example%0Afunction%20exclaim%28message%3A%20string%29%20%7B%0A%09return%20message%20+%20%27%21%27%0A%7D%0Aattempt%28exclaim%29%28%27hi%27%29%0A%20*/%0Afunction%20attempt%3CTFunc%20extends%20%28...args%3A%20any%5B%5D%29%20%3D%3E%20any%3E%28%0A%09/**%0A%09%20*%20The%20function%20to%20wrap%20in%20a%20try...catch.%0A%09%20*/%0A%09func%3A%20TFunc%2C%0A%29%20%7B%0A%09const%20wrapperName%20%3D%20%60attempt%28%24%7Bfunc.name%7D%29%60%0A%09return%20%7B%0A%09%09/**%0A%09%09%20*%20This%20is%20a%20wrapper%20function%20provided%20by%20%60@queso/attempt%60.%0A%09%09%20*%20The%20original%20function%20is%20called%20inside%20of%20a%20try...catch.%0A%09%09%20*/%0A%09%09%5BwrapperName%5D%3A%20%3CTReason%20%3D%20Error%3E%28%0A%09%09%09...args%3A%20Parameters%3CTFunc%3E%0A%09%09%29%3A%20%5BTReason%20%7C%20null%2C%20ReturnType%3CTFunc%3E%3F%5D%20%3D%3E%20%7B%0A%09%09%09try%20%7B%0A%09%09%09%09return%20%5Bnull%2C%20func%28...args%29%5D%0A%09%09%09%7D%20catch%20%28reason%29%20%7B%0A%09%09%09%09return%20%5Breason%5D%0A%09%09%09%7D%0A%09%09%7D%2C%0A%09%7D%5BwrapperName%5D%0A%7D%0A" value="function%20exclaim%28message%29%20%7B%0A%20%20%20%20return%20message%20+%20%27%21%27%3B%0A%7D%0Aattempt%28exclaim%29%28%27hi%27%29%3B%0A" /> ### Result <div id="editor-result" /> ## Parameters | Name | Type | Description | | :----- | :------ | :-------------------- | | `func` | `TFunc` | The function to wrap. | ### Type parameters | Name | Constraint | Default | | :-------- | :------------------------ | :------ | | `TFunc` | `(...args: any[]) => any` | | | `TReason` | | `Error` | ## Returns A new wrapper function that, when called, returns a tuple of `[err, success]`. ### Return type ```ts <TReason = Error>( ...args: Parameters<TFunc> ) => [TReason | null, (ReturnType<TFunc> | undefined)?] ```