{"_id":"@diemantra/safe-await","_rev":"2-0e0727e97999c18bdb021ee2d356c446","name":"@diemantra/safe-await","dist-tags":{"latest":"0.1.1"},"versions":{"0.1.0":{"name":"@diemantra/safe-await","version":"0.1.0","keywords":["promise","async","await","safe","error handling","typescript"],"author":{"name":"Tyson Filia"},"_id":"@diemantra/safe-await@0.1.0","maintainers":[{"name":"diemantra","email":"diemantra@gmail.com"}],"homepage":"https://github.com/tysonjf/safe-await#readme","bugs":{"url":"https://github.com/tysonjf/safe-await/issues"},"dist":{"shasum":"ff0a4227a85b1eb34a68684ca454d7ca82b1fd34","tarball":"https://registry.npmjs.org/@diemantra/safe-await/-/safe-await-0.1.0.tgz","fileCount":8,"integrity":"sha512-8q6OzD5LF7gk3C5eeWRMCFWnt4I3qhY3EEY+yXyhpc2X6QhLUZUFjli7LfJlR/b6hWgC1+6UjuDPX1i8iswmJA==","signatures":[{"sig":"MEUCIQDDeEoLuP8dM8gKrhrXMBi/Hrf50KaY363qowdqELRW1AIgNlKob7G9JwpkCxCoMTYRSNrq3y+PynU6ajGa3hwWlXc=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":9825},"main":"dist/cjs/safeAwait.js","types":"dist/types/safeAwait.d.ts","module":"dist/esm/safeAwait.js","engines":{"node":">=14"},"gitHead":"0d2549501c78e33261c76c9de51ebe8d1b4ef7b8","scripts":{"build":"tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json && tsc -p tsconfig.types.json","prepublishOnly":"npm run build"},"_npmUser":{"name":"diemantra","email":"diemantra@gmail.com"},"repository":{"url":"git+https://github.com/tysonjf/safe-await.git","type":"git"},"_npmVersion":"10.8.2","description":"A utility function to safely await promises with error handling and optional transformations.","directories":{},"_nodeVersion":"18.20.7","_hasShrinkwrap":false,"devDependencies":{"typescript":"^5.0.0","@types/node":"^22.13.14"},"_npmOperationalInternal":{"tmp":"tmp/safe-await_0.1.0_1743374637914_0.31327643618078604","host":"s3://npm-registry-packages-npm-production"}},"0.1.1":{"name":"@diemantra/safe-await","version":"0.1.1","description":"A utility function to safely await promises with error handling and optional transformations.","main":"dist/cjs/safe-await.js","module":"dist/esm/safe-await.js","types":"dist/types/safe-await.d.ts","scripts":{"clean":"rm -rf dist","build:types":"tsc -p tsconfig.types.json","build:js":"esbuild src/safe-await.ts --bundle --outfile=dist/cjs/safe-await.js --format=cjs --platform=node --minify && esbuild src/safe-await.ts --bundle --outfile=dist/esm/safe-await.js --format=esm --platform=node --minify","build":"npm run clean && npm run build:types && npm run build:js","test":"vitest run","test:watch":"vitest","prepublishOnly":"npm run test && npm run build"},"keywords":["promise","async","await","safe","error handling","typescript"],"author":{"name":"Tyson Filia"},"repository":{"type":"git","url":"git+https://github.com/tysonjf/safe-await.git"},"bugs":{"url":"https://github.com/tysonjf/safe-await/issues"},"homepage":"https://github.com/tysonjf/safe-await#readme","devDependencies":{"@types/node":"^22.13.14","esbuild":"^0.20.0","typescript":"^5.0.0","vitest":"^1.0.0"},"engines":{"node":">=14"},"_id":"@diemantra/safe-await@0.1.1","gitHead":"22ba2ad7d3d3d2cf9df76256f1e4cf52b122b090","_nodeVersion":"18.20.7","_npmVersion":"10.8.2","dist":{"integrity":"sha512-rEr3pzTJH0GW1Z2dmxLVjC/02sJzUt3Lfr6VPs278YpvRXQdckiAa4eriCAyN+/9oRVk9AHouFvfVhDBo34mmQ==","shasum":"28b19598593915978bccb764e853de06ad5f880b","tarball":"https://registry.npmjs.org/@diemantra/safe-await/-/safe-await-0.1.1.tgz","fileCount":8,"unpackedSize":7653,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQCYd16Pt1GLq4uqmxTBwa00zlvNeMiMw0oBR1I3mahtMQIhANoYLIcEsemIEsUtHfN9TzvG53lDjGdP+uLXkvG6TvL4"}]},"_npmUser":{"name":"diemantra","email":"diemantra@gmail.com"},"directories":{},"maintainers":[{"name":"diemantra","email":"diemantra@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/safe-await_0.1.1_1743375687700_0.636987298844991"},"_hasShrinkwrap":false}},"time":{"created":"2025-03-30T22:43:57.807Z","modified":"2025-03-30T23:01:28.069Z","0.1.0":"2025-03-30T22:43:58.112Z","0.1.1":"2025-03-30T23:01:27.899Z"},"bugs":{"url":"https://github.com/tysonjf/safe-await/issues"},"author":{"name":"Tyson Filia"},"homepage":"https://github.com/tysonjf/safe-await#readme","keywords":["promise","async","await","safe","error handling","typescript"],"repository":{"type":"git","url":"git+https://github.com/tysonjf/safe-await.git"},"description":"A utility function to safely await promises with error handling and optional transformations.","maintainers":[{"name":"diemantra","email":"diemantra@gmail.com"}],"readme":"# @diemantra/safe-await\n\n[![npm version](https://badge.fury.io/js/%40diemantra%2Fsafe-await.svg)](https://badge.fury.io/js/%40diemantra%2Fsafe-await)\n\nA simple utility function to safely await Promises, providing a consistent way to handle resolved values and errors, inspired by Go's error handling pattern. It also supports optional success/error transformations and timeouts.\n\n## Installation\n\n```bash\nnpm install @diemantra/safe-await\n# or\nyarn add @diemantra/safe-await\n# or\npnpm add @diemantra/safe-await\n```\n\n## Usage\n\nImport the `safeAwait` function:\n\n```typescript\nimport { safeAwait } from '@diemantra/safe-await';\n\nasync function fetchData(url: string): Promise<{ user: string }> {\n\tconst response = await fetch(url);\n\tif (!response.ok) {\n\t\tthrow new Error(`HTTP error! status: ${response.status}`);\n\t}\n\treturn response.json();\n}\n\nasync function main() {\n\tconst url = 'https://api.example.com/data';\n\n\t// Basic usage\n\tconst { data, error } = await safeAwait(fetchData(url));\n\n\tif (error) {\n\t\tconsole.error('Failed to fetch data:', error.message);\n\t\t// Handle the error appropriately\n\t\treturn;\n\t}\n\n\tconsole.log('Fetched data:', data.user);\n\n\t// Usage with success transformation\n\tconst { data: userName, error: fetchError } = await safeAwait(fetchData(url), {\n\t\tonSuccess: (result) => `User name is ${result.user}`,\n\t});\n\n\tif (fetchError) {\n\t\tconsole.error('Failed again:', fetchError.message);\n\t\treturn;\n\t}\n\tconsole.log(userName); // Output: User name is [user's name]\n\n\t// Usage with error transformation\n\ttype CustomError = { code: number; message: string };\n\tconst { error: customError } = await safeAwait(fetchData('invalid-url'), {\n\t\tonError: (err): CustomError => ({\n\t\t\tcode: 500,\n\t\t\tmessage: `Transformed error: ${err.message}`,\n\t\t}),\n\t});\n\n\tif (customError) {\n\t\tconsole.error(`Error Code: ${customError.code}, Message: ${customError.message}`);\n\t}\n\n\t// Usage with timeout\n\tconst { error: timeoutError } = await safeAwait(fetchData(url), {\n\t\ttimeoutMs: 10, // Very short timeout for demonstration\n\t});\n\n\tif (timeoutError) {\n\t\tconsole.error('Operation timed out:', timeoutError.message);\n\t}\n}\n\nmain();\n```\n\n## API\n\n### `safeAwait<T, E = Error, M = T, N = E>(promise: Promise<T>, options?: Options): Promise<Result<M, N>>`\n\n- `promise`: The `Promise<T>` to await.\n- `options` (optional):\n  - `onSuccess?: (data: T) => M | Promise<M>`: An optional function to transform the data if the promise resolves successfully. Can be async.\n  - `onError?: (error: E) => N`: An optional function to transform the error if the promise rejects. **Note:** This function _receives_ the error but should _return_ the transformed error value (`N`) to be placed in the `.error` property of the result. It does _not_ re-throw the error.\n  - `timeoutMs?: number`: An optional timeout in milliseconds. If the promise doesn't resolve or reject within this time, it will reject with a timeout error.\n- Returns: `Promise<Result<M, N>>` where `Result` is an object ` { data: M } | { error: N }`.\n# safe-await\n# safe-await\n","readmeFilename":"README.md"}