{"_id":"@cmchu/lazy-get-result","name":"@cmchu/lazy-get-result","dist-tags":{"latest":"0.0.1"},"versions":{"0.0.1":{"name":"@cmchu/lazy-get-result","author":{"name":"cmchu"},"version":"0.0.1","license":"MIT","description":"A lightweight utility for implementing lazy evaluation in JavaScript/TypeScript applications. This module provides a function to wrap any computationally expensive operation, ensuring it's executed only once and its result is cached for all subsequent cal","keywords":["lazy evaluation","memoization","performance optimization","function caching","utility","typescript","javascript"],"type":"module","main":"./dist/index.umd.cjs","module":"./dist/index.js","types":"./index.d.ts","exports":{"types":"./index.d.ts","import":"./dist/index.js","require":"./dist/index.umd.cjs"},"publishConfig":{"access":"public","registry":"https://registry.npmjs.org/"},"scripts":{"build":"tsc && vite build"},"devDependencies":{"typescript":"^5.4.5","vite":"^5.2.10"},"_id":"@cmchu/lazy-get-result@0.0.1","gitHead":"61429a05d2ff7e5c9abe279bb929026ae174e698","_nodeVersion":"21.1.0","_npmVersion":"10.2.0","dist":{"integrity":"sha512-A/LjKIBxFnMOyYyxhQk+vnqvZW6zqJhVLEaLYu9q/KUX56UcurxVfJnCWSNe09vc/y/9xQym9zdiu6eHq0yZlg==","shasum":"2489c7ee80ebc6fb802234ff6fcfa538b9e74ee1","tarball":"https://registry.npmjs.org/@cmchu/lazy-get-result/-/lazy-get-result-0.0.1.tgz","fileCount":6,"unpackedSize":5151,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIAGj7WYhV+LBs+uRv74YDLb/B4b4VulnXgXX7YjZmrd9AiEAyQ+aXS7uaNG8m8TaN4OmjeX9ghdJKrW+wE/sSqjj4Xg="}]},"_npmUser":{"name":"chuchengmin","email":"15656552925@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/lazy-get-result_0.0.1_1719043125185_0.6241093938147915"},"_hasShrinkwrap":false}},"time":{"created":"2024-06-22T07:58:45.070Z","0.0.1":"2024-06-22T07:58:45.319Z","modified":"2024-06-22T07:58:45.661Z"},"maintainers":[{"name":"chuchengmin","email":"15656552925@163.com"}],"description":"A lightweight utility for implementing lazy evaluation in JavaScript/TypeScript applications. This module provides a function to wrap any computationally expensive operation, ensuring it's executed only once and its result is cached for all subsequent cal","keywords":["lazy evaluation","memoization","performance optimization","function caching","utility","typescript","javascript"],"author":{"name":"cmchu"},"license":"MIT","readme":"# Lazy Evaluation Function\r\n\r\n## Overview\r\n\r\nThis document describes the functionality and usage of the `lazyGetResult` utility function. The purpose of this function is to facilitate lazy loading and caching of results from an expensive or time-consuming computation. It ensures that the provided function (`fn`) is executed only once, upon the first call, and subsequent calls simply return the cached result.\r\n\r\n## Usage\r\n\r\n### Installation\r\n\r\n```bash\r\nnpm install @cmchu/lazy-get-result --save\r\n```\r\n\r\n### Example\r\n\r\n```javascript\r\nimport lazyGetResult from '@cmchu/lazy-get-result';\r\n\r\n// An expensive computation function\r\nfunction complexComputation() {\r\n  console.log('Computing complex result...');\r\n  // Simulated long-running process\r\n  const result = performExpensiveCalculation();\r\n  return result;\r\n}\r\n\r\n// Wrapping with lazyGetResult\r\nconst lazyComplexComputation = lazyGetResult(complexComputation);\r\n\r\n// First call will execute the function and log \"Computing complex result...\"\r\nconsole.log(lazyComplexComputation());\r\n\r\n// Subsequent calls will not recompute, just return the cached result\r\nconsole.log(lazyComplexComputation());\r\n```\r\n\r\n## API\r\n\r\n### `lazyGetResult(fn: (...args: any[]) => T): () => T`\r\n\r\n#### Parameters\r\n\r\n- `fn`: A function of type `(...args: any[]) => T`. This is the function you want to lazily evaluate and cache its result.\r\n\r\n#### Returns\r\n\r\nA new function of type `() => T`. When invoked for the first time, this function will execute `fn`, cache its result, and return that result. All further invocations of this returned function will yield the cached result without re-executing `fn`.\r\n\r\n#### Note\r\n\r\n- Error handling is integrated within `lazyGetResult`. If `fn` throws an error during its first execution, the error is caught, and the `called` flag is reset to `false`, ensuring that the function can be retried.\r\n\r\n## Conclusion\r\n\r\nThe `lazyGetResult` utility promotes efficient resource utilization by deferring and caching the outcome of potentially costly operations. It is particularly beneficial in scenarios where an operation's output is needed but can be computationally intensive or involve external service calls, making it ideal for optimizing performance in web applications, data processing pipelines, or any environment where reducing redundant computations is crucial.","readmeFilename":"README.md"}