{"_id":"@adam-rocska/until","_rev":"2-4fc79270366510feb65b27b68b04b6dd","name":"@adam-rocska/until","dist-tags":{"latest":"1.0.1"},"versions":{"1.0.0":{"name":"@adam-rocska/until","version":"1.0.0","keywords":["typescript","async","promise","timeout","predicate","retry","javascript","polling","await","node","browser"],"author":{"url":"https://adam-rocska.github.io","name":"Ádám László Rocska"},"license":"MIT","_id":"@adam-rocska/until@1.0.0","maintainers":[{"name":"adam-rocska","email":"rocska.adam@gmail.com"}],"homepage":"https://github.com/adam-rocska/unti-typescript#readme","bugs":{"url":"https://github.com/adam-rocska/until-typescript/issues"},"dist":{"shasum":"5bef2a705e875ff72e406e1d3c23af8702dfd351","tarball":"https://registry.npmjs.org/@adam-rocska/until/-/until-1.0.0.tgz","fileCount":6,"integrity":"sha512-j+U9tqFpxJohFaioFjPelPivfxvjXuLtx5RYW0Dyyipuw4jqA+meE2oNDaGEH8UbWttIzeXbY9ULzUM+PcUY3A==","signatures":[{"sig":"MEUCIQDWxy6urzcxcwqZJ8h6S+bEM6XKdkHxGHP6RpbFcXoAOwIgReKwx41EX+OxFuw3fWA3xt1ojHFlHVwSI6Ljc/rKezo=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":13210},"jest":{"transform":{"^.+\\.(t|j)sx?$":["@swc/jest"]},"testTimeout":1000,"moduleNameMapper":{"^!(.*)":"<rootDir>/$1","^@adam-rocska/until$":"<rootDir>/src/index.ts"},"moduleDirectories":["node_modules"],"collectCoverageFrom":["src/**/*.ts"],"testPathIgnorePatterns":["/node_modules/"]},"main":"./dist/cjs/index.js","_from":"file:adam-rocska-until-1.0.0.tgz","types":"./dist/cjs/index.d.ts","module":"./dist/es/index.mjs","funding":[{"url":"https://github.com/sponsors/adam-rocska","type":"github"}],"scripts":{"test":"jest --env node","build":"bunchee","check":"pnpm eslint && pnpm attw --pack","clean":"rimraf dist"},"_npmUser":{"name":"adam-rocska","email":"rocska.adam@gmail.com"},"_resolved":"/private/var/folders/05/vbrh322d2tx9581k6m09rm340000gn/T/074b991c0802967cc9d36e350ff426b7/adam-rocska-until-1.0.0.tgz","_integrity":"sha512-j+U9tqFpxJohFaioFjPelPivfxvjXuLtx5RYW0Dyyipuw4jqA+meE2oNDaGEH8UbWttIzeXbY9ULzUM+PcUY3A==","repository":{"url":"git+https://github.com/adam-rocska/unti-typescript.git","type":"git"},"_npmVersion":"10.2.3","description":"A lightweight TypeScript utility to repeatedly execute an asynchronous predicate until it resolves to true or times out.","directories":{},"_nodeVersion":"20.10.0","browserslist":["last 3 years"],"publishConfig":{"access":"public","registry":"https://registry.npmjs.org/"},"_hasShrinkwrap":false,"packageManager":"pnpm@10.11.0","devDependencies":{"jest":"^29.7.0","pnpm":"^10.11.0","eslint":"^8.57.1","rimraf":"^6.0.1","semver":"^7.7.2","bunchee":"^6.5.2","@swc/jest":"^0.2.38","typescript":"^5.8.3","@types/jest":"^29.5.14","@types/node":"^22.15.21","eslint-config-next":"15.3.2","eslint-plugin-node":"^11.1.0","@arethetypeswrong/cli":"^0.18.1","@typescript-eslint/parser":"^8.32.1","@typescript-eslint/eslint-plugin":"^8.32.1"},"_npmOperationalInternal":{"tmp":"tmp/until_1.0.0_1747839152925_0.9404119751715312","host":"s3://npm-registry-packages-npm-production"}},"1.0.1":{"name":"@adam-rocska/until","author":{"name":"Ádám László Rocska","url":"https://adam-rocska.github.io"},"version":"1.0.1","description":"A lightweight TypeScript utility to repeatedly execute an asynchronous predicate until it resolves to true or times out.","keywords":["typescript","async","promise","timeout","predicate","retry","javascript","polling","await","node","browser"],"browserslist":["last 3 years"],"main":"./dist/cjs/index.js","module":"./dist/es/index.mjs","types":"./dist/cjs/index.d.ts","publishConfig":{"access":"public","registry":"https://registry.npmjs.org/"},"repository":{"type":"git","url":"git+https://github.com/adam-rocska/until-typescript.git"},"bugs":{"url":"https://github.com/adam-rocska/until-typescript/issues"},"funding":[{"type":"github","url":"https://github.com/sponsors/adam-rocska"}],"license":"MIT","devDependencies":{"@arethetypeswrong/cli":"^0.18.1","@swc/jest":"^0.2.38","@types/jest":"^29.5.14","@types/node":"^22.15.21","@typescript-eslint/eslint-plugin":"^8.32.1","@typescript-eslint/parser":"^8.32.1","bunchee":"^6.5.2","eslint":"^8.57.1","eslint-config-next":"15.3.2","eslint-plugin-node":"^11.1.0","jest":"^29.7.0","pnpm":"^10.11.0","rimraf":"^6.0.1","semver":"^7.7.2","typescript":"^5.8.3"},"jest":{"moduleDirectories":["node_modules"],"moduleNameMapper":{"^@adam-rocska/until$":"<rootDir>/src/index.ts","^!(.*)":"<rootDir>/$1"},"transform":{"^.+\\.(t|j)sx?$":["@swc/jest"]},"testPathIgnorePatterns":["/node_modules/"],"collectCoverageFrom":["src/**/*.ts"],"testTimeout":1000},"scripts":{"clean":"rimraf dist","build":"bunchee","test":"jest --env node","check":"pnpm eslint && pnpm attw --pack"},"_id":"@adam-rocska/until@1.0.1","homepage":"https://github.com/adam-rocska/until-typescript#readme","_integrity":"sha512-Y5GJxTuZNh2NHZfhBvqrgzmeKbD90ayBXzJC0fcDgXsjaVSTlTO4O9UbMHGnLgrPj/KR5z8M0Bcsd7tdq8v6hQ==","_resolved":"/tmp/d3078dc2a6041fbc1b9dcce894e8ceda/adam-rocska-until-1.0.1.tgz","_from":"file:adam-rocska-until-1.0.1.tgz","_nodeVersion":"20.19.1","_npmVersion":"10.8.2","dist":{"integrity":"sha512-Y5GJxTuZNh2NHZfhBvqrgzmeKbD90ayBXzJC0fcDgXsjaVSTlTO4O9UbMHGnLgrPj/KR5z8M0Bcsd7tdq8v6hQ==","shasum":"6f1985238115b630f84384d1db89ac694a3c2e06","tarball":"https://registry.npmjs.org/@adam-rocska/until/-/until-1.0.1.tgz","fileCount":6,"unpackedSize":13175,"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@adam-rocska%2funtil@1.0.1","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIBzGK/fseC2Q6PQ48J1jgrFNhllDmhmcp00wrL9wzkDBAiEAur5OM97RyA9ofcINbK64CBimc+iUPVtRepItIoyt2aE="}]},"_npmUser":{"name":"adam-rocska","email":"rocska.adam@gmail.com"},"directories":{},"maintainers":[{"name":"adam-rocska","email":"rocska.adam@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/until_1.0.1_1747841239404_0.11168791484313179"},"_hasShrinkwrap":false}},"time":{"created":"2025-05-21T14:52:32.802Z","modified":"2025-05-21T15:27:19.978Z","1.0.0":"2025-05-21T14:52:33.144Z","1.0.1":"2025-05-21T15:27:19.579Z"},"bugs":{"url":"https://github.com/adam-rocska/until-typescript/issues"},"author":{"name":"Ádám László Rocska","url":"https://adam-rocska.github.io"},"license":"MIT","homepage":"https://github.com/adam-rocska/until-typescript#readme","keywords":["typescript","async","promise","timeout","predicate","retry","javascript","polling","await","node","browser"],"repository":{"type":"git","url":"git+https://github.com/adam-rocska/until-typescript.git"},"description":"A lightweight TypeScript utility to repeatedly execute an asynchronous predicate until it resolves to true or times out.","maintainers":[{"name":"adam-rocska","email":"rocska.adam@gmail.com"}],"readme":"# @adam-rocska/until\n\nA lightweight TypeScript/JavaScript utility for repeatedly executing an asynchronous predicate function until it returns `true` or a specified timeout is reached. If the timeout is exceeded, the promise rejects with a `TimeoutError`.\n\n[![NPM Version](https://img.shields.io/npm/v/@adam-rocska/until.svg)](https://www.npmjs.com/package/@adam-rocska/until)\n[![License](https://img.shields.io/npm/l/@adam-rocska/until)](https://github.com/adam-rocska/until-typescript/blob/master/LICENSE)\n\n| Aspect               | Badge                                                                                                                                           |\n| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |\n| Minified             | [![Minified](https://badgen.net/bundlephobia/min/@adam-rocska/until)](https://bundlephobia.com/package/@adam-rocska/until)                      |\n| Minified + gzip      | [![Minified + gzip](https://badgen.net/bundlephobia/minzip/@adam-rocska/until)](https://bundlephobia.com/package/@adam-rocska/until)            |\n| Dependency Count     | [![Dependency Count](https://badgen.net/bundlephobia/dependency-count/@adam-rocska/until)](https://bundlephobia.com/package/@adam-rocska/until) |\n| Tree-shaking Support | [![Tree-shaking Support](https://badgen.net/bundlephobia/tree-shaking/@adam-rocska/until)](https://bundlephobia.com/package/@adam-rocska/until) |\n\n## Features\n\n- **Asynchronous Predicate Execution**: Repeatedly runs an async predicate function until it returns `true`.\n- **Configurable Timeout and Retry Interval**: Customize the maximum wait time and retry interval.\n- **Error Handling**: Rejects with a custom `TimeoutError` when the timeout is reached.\n- **TypeScript Support**: Fully typed for a better developer experience.\n- **Lightweight**: Zero runtime dependencies and a minimal footprint.\n- **Browser and Node.js Support**: Compatible with modern browsers (last 3 years) and Node.js (v14+).\n- **Well-Tested**: Includes unit tests for early resolution, timeout rejection, and predicate fulfillment.\n\n## Installation\n\nInstall the package via npm or pnpm:\n\n```bash\nnpm install @adam-rocska/until\n```\n\nor\n\n```bash\npnpm add @adam-rocska/until\n```\n\n## Usage\n\nThe `until` function takes a predicate function, an optional timeout (in milliseconds), and an optional retry interval (in milliseconds). It returns a `Promise` that resolves when the predicate returns `true` or rejects with a `TimeoutError` if the timeout is exceeded.\n\n### Example: Basic Usage\n\n```javascript\nimport { until } from '@adam-rocska/until';\n\n// Example predicate: checks if a condition is met\nasync function isReady() {\n  // Simulate an async operation (e.g., checking if a resource is available)\n  return Math.random() > 0.8; // Returns true ~20% of the time\n}\n\nasync function main() {\n  try {\n    await until(isReady, 5000, 100);\n    console.log('Condition met!');\n  } catch (error) {\n    console.error('Failed:', error.message);\n  }\n}\n\nmain();\n```\n\n### Example: Handling TimeoutError\n\n```javascript\nimport { until, TimeoutError } from '@adam-rocska/until';\n\nasync function main() {\n  try {\n    await until(async () => false, 1000, 100); // Will timeout after 1 second\n  } catch (error) {\n    if (error instanceof TimeoutError) {\n      console.error(`Timeout after ${error.timeout}ms with retry interval ${error.retry}ms`);\n    }\n  }\n}\n\nmain();\n```\n\n### Parameters\n\n| Parameter   | Type        | Description                                                                   | Default |\n| ----------- | ----------- | ----------------------------------------------------------------------------- | ------- |\n| `predicate` | `Predicate` | An async function that returns a boolean or a Promise resolving to a boolean. | -       |\n| `timeout`   | `number`    | Maximum time (in milliseconds) to wait for the predicate to return `true`.    | `5000`  |\n| `retry`     | `number`    | Interval (in milliseconds) between each execution of the predicate.           | `100`   |\n\n### Returns\n\n- **Resolves**: `Promise<void>` - Resolves when the predicate returns `true`.\n- **Rejects**: `Promise<TimeoutError>` - Rejects with a `TimeoutError` if the timeout is exceeded.\n\n### Types\n\nThe library includes TypeScript definitions for type safety.\n\n```typescript\ntype Predicate = () => boolean | Promise<boolean>;\n\nclass TimeoutError extends Error {\n  constructor(message: string, timeout: number, retry: number, predicate: Predicate);\n  timeout: number;\n  retry: number;\n  predicate: Predicate;\n}\n```\n\n## API\n\n### `until(predicate: Predicate, timeout?: number, retry?: number): Promise<void>`\n\nExecutes the provided `predicate` function repeatedly until it returns `true` or the `timeout` is reached. The function is executed every `retry` milliseconds.\n\n- **Throws**: `TimeoutError` if the timeout is exceeded.\n\n## Requirements\n\n- **Node.js**: Version 14 or higher (due to async/await and modern JavaScript features).\n- **Browsers**: Supports browsers from the last 3 years (see `browserslist` in `package.json`).\n- **TypeScript**: Optional, for type safety (version 5.8 or higher recommended).\n\n## Development\n\nTo contribute or test the library locally:\n\n1. Clone the repository:\n\n   ```bash\n   git clone https://github.com/adam-rocska/until-typescript.git\n   ```\n\n2. Install dependencies using pnpm:\n\n   ```bash\n   pnpm install\n   ```\n\n3. Run tests:\n\n   ```bash\n   pnpm test\n   ```\n\n4. Build the library:\n\n   ```bash\n   pnpm build\n   ```\n\n5. Check code quality and types:\n\n   ```bash\n   pnpm check\n   ```\n\nThe library uses Jest for testing, ESLint for linting, and `bunchee` for building ES and CommonJS modules.\n\n## Testing\n\nThe library includes unit tests covering the following scenarios:\n\n- Early resolution when the predicate returns `true` immediately.\n- Rejection with `TimeoutError` when the predicate never returns `true`.\n- Successful resolution when the predicate returns `true` within the timeout.\n\nRun `pnpm test` to execute the test suite.\n\n## License\n\nThis project is licensed under the [MIT License](LICENSE).\n\n## Contributing\n\nContributions are welcome! Please open an issue or submit a pull request on the [GitHub repository](https://github.com/adam-rocska/until-typescript).\n\n## Support\n\nIf you encounter issues or have questions, please file an issue on the [GitHub Issues page](https://github.com/adam-rocska/until-typescript/issues).\n\n## Funding\n\nSupport the development of this project via [GitHub Sponsors](https://github.com/sponsors/adam-rocska).\n\n## Author\n\nCreated by [Ádám László Rocska](https://adam-rocska.github.io).\n","readmeFilename":"README.md"}