diff --git a/npmextra.json b/npmextra.json index acab353..f24705c 100644 --- a/npmextra.json +++ b/npmextra.json @@ -9,12 +9,24 @@ "githost": "code.foss.global", "gitscope": "push.rocks", "gitrepo": "smartdelay", - "description": "timeouts for the async/await era, written in TypeScript", + "description": "A TypeScript library providing enhanced timeout functions compatible with async/await patterns.", "npmPackagename": "@push.rocks/smartdelay", - "license": "MIT" + "license": "MIT", + "keywords": [ + "TypeScript", + "async/await", + "timeouts", + "delay", + "scheduling", + "task delay", + "asynchronous", + "programming utility", + "promise", + "timeout management" + ] } }, - "tsdocs": { + "tsdoc": { "legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n" } } \ No newline at end of file diff --git a/package.json b/package.json index b5367e1..aff72e6 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@push.rocks/smartdelay", "private": false, "version": "3.0.5", - "description": "timeouts for the async/await era, written in TypeScript", + "description": "A TypeScript library providing enhanced timeout functions compatible with async/await patterns.", "main": "dist_ts/index.js", "typings": "dist_ts/index.d.ts", "scripts": { @@ -46,5 +46,17 @@ "browserslist": [ "last 1 chrome versions" ], - "type": "module" -} + "type": "module", + "keywords": [ + "TypeScript", + "async/await", + "timeouts", + "delay", + "scheduling", + "task delay", + "asynchronous", + "programming utility", + "promise", + "timeout management" + ] +} \ No newline at end of file diff --git a/readme.hints.md b/readme.hints.md new file mode 100644 index 0000000..0519ecb --- /dev/null +++ b/readme.hints.md @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/readme.md b/readme.md index 3ba9732..0174ee9 100644 --- a/readme.md +++ b/readme.md @@ -1,50 +1,117 @@ # @push.rocks/smartdelay -timeouts for the async/await era, written in TypeScript -## Availabililty and Links -* [npmjs.org (npm package)](https://www.npmjs.com/package/@push.rocks/smartdelay) -* [gitlab.com (source)](https://gitlab.com/pushrocks/smartdelay) -* [github.com (source mirror)](https://github.com/pushrocks/smartdelay) -* [docs (typedoc)](https://pushrocks.gitlab.io/smartdelay/) +@push.rocks/smartdelay is a modern library designed to simplify working with timeouts in the async/await era, all while being fully written in TypeScript. This tool offers a range of functionalities that streamline the process of implementing delays and timeouts in your asynchronous JavaScript code, making it more readable and maintainable. -## Status for master +## Install -Status Category | Status Badge --- | -- -GitLab Pipelines | [![pipeline status](https://gitlab.com/pushrocks/smartdelay/badges/master/pipeline.svg)](https://lossless.cloud) -GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/pushrocks/smartdelay/badges/master/coverage.svg)](https://lossless.cloud) -npm | [![npm downloads per month](https://badgen.net/npm/dy/@push.rocks/smartdelay)](https://lossless.cloud) -Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/pushrocks/smartdelay)](https://lossless.cloud) -TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud) -node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/) -Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud) -PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@push.rocks/smartdelay)](https://lossless.cloud) -PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@push.rocks/smartdelay)](https://lossless.cloud) -BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@push.rocks/smartdelay)](https://lossless.cloud) +To integrate @push.rocks/smartdelay into your project, you can install it via npm. Run the following command in your project directory: + +```bash +npm install @push.rocks/smartdelay --save +``` + +This command adds the package to your project's dependencies, ensuring that you can easily import and utilize smartdelay's functions in your TypeScript files. ## Usage -Use TypeScript for best in class instellisense. +@push.rocks/smartdelay simplifies the handling of timeouts within async functions, offering methods to introduce specified delays or randomized time intervals. Below are detailed examples to demonstrate its usage. These examples are crafted using ECMAScript Modules (ESM) syntax and TypeScript. -```javascript -import * as smartdelay from 'smartdelay'; +### Basic Delay -(async () => { - await smartdelay.delayFor(3000); // excution will halt here 3 seconds for this function scope BUT NOT BLOCK anything else - console.log('hi there'); +To introduce a basic delay in your asynchronous function, use the `delayFor` function. This method halts the execution for a specified number of milliseconds. - // You can also go random - await smartdelay.delayForRandom(2000, 6000); // this will delay exection somewhere between 2 and 6 seconds. - console.log('Yay. You did not see me coming at this exact time'); -})(); +```typescript +import { delayFor } from '@push.rocks/smartdelay'; + +async function basicDelayExample() { + console.log('Delay start'); + await delayFor(3000); // Execution will pause here for 3 seconds + console.log('Delay ended'); +} + +basicDelayExample(); ``` -## Contribution +In the above example, the program prints "Delay start", waits for 3 seconds due to `delayFor`, and then prints "Delay ended". -We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :) +### Delay with Randomization -For further information read the linked docs at the top of this readme. +For scenarios where you need a delay within a random time range, `delayForRandom` can be utilized. This introduces a non-deterministic delay duration, making it ideal for simulating real-world scenarios or for testing purposes. -## Legal -> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc) -| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy) +```typescript +import { delayForRandom } from '@push.rocks/smartdelay'; + +async function randomDelayExample() { + console.log('Random delay start'); + await delayForRandom(2000, 5000); // Delay execution for a random duration between 2 and 5 seconds + console.log('Random delay ended'); +} + +randomDelayExample(); +``` + +This function takes two parameters: the minimum and maximum bounds (in milliseconds) for the random delay. + +### Passing Through Values + +Both `delayFor` and `delayForRandom` can be used to pass through values after the delay. This feature can be particularly useful when chaining asynchronous operations. + +```typescript +import { delayFor } from '@push.rocks/smartdelay'; + +async function passThroughExample() { + const result = await delayFor(3000, 'Hello after delay'); + console.log(result); // Outputs: Hello after delay +} + +passThroughExample(); +``` + +### Advanced Usage: Timeout Class + +@push.rocks/smartdelay provides a `Timeout` class for more granular control over timeouts, including support for cancellation. + +```typescript +import { Timeout } from '@push.rocks/smartdelay'; + +async function timeoutExample() { + const timeout = new Timeout(5000, 'Result after 5 seconds'); + // Cancel the timeout if needed + // timeout.cancel(); + + try { + const result = await timeout.promise; + console.log(result); // Result after 5 seconds (if not cancelled) + } catch (error) { + console.error('Timeout was cancelled', error); + } +} + +timeoutExample(); +``` + +This class allows you to programmatically cancel the timeout before it completes, providing flexibility for dynamic timeout management situations. + +## Conclusion + +@push.rocks/smartdelay offers a TypeScript-friendly, easy-to-use solution for managing timeouts and delays in asynchronous JavaScript. By leveraging this module, developers can write cleaner, more readable async code with minimal boilerplate. Whether you're implementing a simple delay, a random delay, or need finer control over your timeout logic, smartdelay provides the tools you need to get the job done effectively. + + +## License and Legal Information + +This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. + +**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file. + +### Trademarks + +This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH. + +### Company Information + +Task Venture Capital GmbH +Registered at District court Bremen HRB 35230 HB, Germany + +For any legal inquiries or if you require further information, please contact us via email at hello@task.vc. + +By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.