@parasrp/delay 1.1.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +9 -0
  2. package/README.md +32 -14
  3. package/index.js +28 -7
  4. package/package.json +24 -5
package/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Paras R Poshiya
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -2,21 +2,22 @@
2
2
 
3
3
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4
4
  ![npm version](https://img.shields.io/npm/v/@parasrp/delay)
5
- ![Size](https://img.shields.io/bundlephobia/min/@parasrp/delay)
5
+ ![npm downloads](https://img.shields.io/npm/dw/@parasrp/delay)
6
+ ![install size](https://packagephobia.com/badge?p=@parasrp/delay)
7
+ ![bundle size](https://img.shields.io/bundlephobia/min/@parasrp/delay)
6
8
 
7
9
  A lightweight, promise-based JavaScript utility to pause execution for a specified duration. Perfect for adding timeouts, staggering API calls, or building simple animations.
8
10
 
9
11
  ---
10
-
11
12
  ## ✨ Features
12
13
 
13
14
  - đŸĒļ **Lightweight & Fast**: Zero overhead, minimal footprint.
14
15
  - 🤝 **Promise-based**: Works seamlessly with `async/await`.
15
16
  - đŸ“Ļ **Zero Dependencies**: Keeps your node_modules clean.
16
17
  - 🌍 **Universal**: Works in Node.js, browsers, and modern JS environments.
18
+ - âąī¸ **Flexible Units**: Supports `milliseconds`, `seconds`, `minutes`, and `hours`.
17
19
 
18
20
  ---
19
-
20
21
  ## đŸ“Ļ Installation
21
22
 
22
23
  Install the package via npm:
@@ -28,8 +29,8 @@ Install the package via yarn:
28
29
  ```bash
29
30
  yarn add @parasrp/delay
30
31
  ```
31
- ---
32
32
 
33
+ ---
33
34
  ## ✨ Features
34
35
  You can use @parasrp/delay with both CommonJS and ES Modules.
35
36
 
@@ -44,23 +45,34 @@ const delay = require("@parasrp/delay");
44
45
  ```
45
46
 
46
47
  ---
48
+ ## 📖 API
49
+
50
+ ```js
51
+ /**
52
+ * Sleep (pause execution) for a given time with unit
53
+ * @param {number} value - The amount of time to wait
54
+ * @param {'milliseconds'|'seconds'|'minutes'|'hours'} [unit='milliseconds'] - The unit of time (default is milliseconds)
55
+ * @returns {Promise<void>} - A Promise that resolves after the specified delay
56
+ **/
57
+ await delay(value, unit?);
58
+ ```
47
59
 
60
+ ---
48
61
  ## Examples
49
62
 
50
63
  Using async/await:
51
64
  ```js
52
- async function countdown() {
53
- console.log("Starting countdown...");
54
-
55
- for (let i = 5; i > 0; i--) {
56
- console.log(i);
57
- await delay(1000); // Waits for 1 second
58
- }
59
-
60
- console.log("Go!");
65
+ async function demo() {
66
+ console.log("Waiting 2 seconds...");
67
+ await delay(2, "seconds"); // waits 2000 ms
68
+ console.log("Done!");
69
+
70
+ console.log("Waiting 1 minute...");
71
+ await delay(1, "minutes"); // waits 60000 ms
72
+ console.log("Finished!");
61
73
  }
62
74
 
63
- countdown();
75
+ demo();
64
76
  ```
65
77
 
66
78
  Using .then():
@@ -70,8 +82,14 @@ console.log("Start waiting...");
70
82
  delay(1000).then(() => {
71
83
  console.log("Finished waiting 1000 milliseconds");
72
84
  });
85
+
86
+ delay(0.5, "hours").then(() => {
87
+ console.log("Finished waiting 30 minutes");
88
+ });
89
+
73
90
  ```
74
91
 
92
+ ---
75
93
  ## 👤 Author
76
94
  **Paras R Poshiya**
77
95
  * [GitHub](https://github.com/parasposhiya)
package/index.js CHANGED
@@ -1,10 +1,31 @@
1
1
  /**
2
- * Sleep for given milliseconds
3
- * @param {number} ms
4
- * @returns {Promise<void>}
5
- */
2
+ * Sleep (pause execution) for a given time with unit
3
+ * @param {number} value - The amount of time to wait
4
+ * @param {'milliseconds'|'seconds'|'minutes'|'hours'} [unit='milliseconds'] - The unit of time (default is milliseconds)
5
+ * @returns {Promise<void>} - A Promise that resolves after the specified delay
6
+ **/
7
+ const delay = (value, unit = 'milliseconds') => {
8
+ let ms; // variable to hold the converted time in milliseconds
6
9
 
7
- const delay = (ms) =>
8
- new Promise((resolve) => setTimeout(resolve, ms));
10
+ // Convert the given value into milliseconds based on the unit
11
+ switch (unit) {
12
+ case 'seconds':
13
+ ms = value * 1000; // 1 second = 1000 ms
14
+ break;
15
+ case 'minutes':
16
+ ms = value * 1000 * 60; // 1 minute = 60,000 ms
17
+ break;
18
+ case 'hours':
19
+ ms = value * 1000 * 60 * 60; // 1 hour = 3,600,000 ms
20
+ break;
21
+ case 'milliseconds':
22
+ default:
23
+ ms = value; // already in milliseconds
24
+ }
9
25
 
10
- export default delay
26
+ // Return a Promise that resolves after the calculated milliseconds
27
+ return new Promise((resolve) => setTimeout(resolve, ms));
28
+ };
29
+
30
+ // Export the function so it can be imported in other files
31
+ export default delay;
package/package.json CHANGED
@@ -1,16 +1,35 @@
1
1
  {
2
2
  "name": "@parasrp/delay",
3
- "version": "1.1.0",
4
- "description": "sleep function",
3
+ "version": "2.0.0",
4
+ "description": "A simple sleep/delay function with support for milliseconds, seconds, minutes, and hours.",
5
5
  "author": "Paras R Poshiya",
6
6
  "keywords": [
7
7
  "sleep",
8
- "delay"
8
+ "delay",
9
+ "wait",
10
+ "timeout",
11
+ "pause"
9
12
  ],
10
13
  "license": "MIT",
11
14
  "main": "index.js",
12
15
  "type": "module",
13
16
  "exports": {
14
17
  ".": "./index.js"
15
- }
16
- }
18
+ },
19
+ "scripts": {
20
+ "test": "node --test",
21
+ "build": "echo 'No build step yet' && exit 0"
22
+ },
23
+ "files": [
24
+ "index.js",
25
+ "README.md"
26
+ ],
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "https://gitlab.com/parasrp-npm/delay.git"
30
+ },
31
+ "bugs": {
32
+ "url": "https://gitlab.com/parasrp-npm/delay/issues"
33
+ },
34
+ "homepage": "https://gitlab.com/parasrp-npm/delay#readme"
35
+ }