waiit 0.0.1
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.
- package/LICENSE +21 -0
- package/README.md +137 -0
- package/app.js +1 -0
- package/index.d.ts +10 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 VARInc
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# waiit
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/waiit)
|
|
4
|
+
[](https://github.com/Raju/waiit/blob/main/LICENSE)
|
|
5
|
+
|
|
6
|
+
A lightweight, zero-dependency utility to wait for any amount of time in milliseconds. Returns a promise that resolves after the specified delay, or immediately if no value is passed.
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- 🪶 **Lightweight** - Single line of code, zero dependencies
|
|
11
|
+
- ⚡ **Simple API** - Easy to use with async/await
|
|
12
|
+
- 📦 **TypeScript Support** - Includes type definitions
|
|
13
|
+
- 🔧 **Flexible** - Pass any millisecond value or omit for immediate resolution
|
|
14
|
+
- 🌐 **Universal** - Works in Node.js and browsers
|
|
15
|
+
|
|
16
|
+
## Platform Support
|
|
17
|
+
|
|
18
|
+
| Platform | Supported |
|
|
19
|
+
|----------|-----------|
|
|
20
|
+
| Node.js | ✅ |
|
|
21
|
+
| Browser | ✅ |
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install waiit
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## API
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
wait(ms?: number): Promise<void>
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
| Parameter | Type | Default | Description |
|
|
36
|
+
|-----------|----------|---------|--------------------------------------|
|
|
37
|
+
| `ms` | `number` | `0` | Time to wait in milliseconds |
|
|
38
|
+
|
|
39
|
+
**Returns:** A `Promise` that resolves after the specified time.
|
|
40
|
+
|
|
41
|
+
## Examples
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
wait() // Returns a promise that resolves immediately
|
|
45
|
+
wait(1000) // Returns a promise that resolves after 1 second
|
|
46
|
+
wait(5000) // Returns a promise that resolves after 5 seconds
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
|
|
51
|
+
### CommonJS
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
const wait = require('waiit');
|
|
55
|
+
|
|
56
|
+
const doSomething = async () => {
|
|
57
|
+
console.log('Start');
|
|
58
|
+
|
|
59
|
+
await wait(5000);
|
|
60
|
+
console.log('Waited 5 seconds');
|
|
61
|
+
|
|
62
|
+
await wait();
|
|
63
|
+
console.log('Finish');
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
doSomething();
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### ES Modules
|
|
70
|
+
|
|
71
|
+
```js
|
|
72
|
+
import wait from 'waiit';
|
|
73
|
+
|
|
74
|
+
async function example() {
|
|
75
|
+
console.log('Starting...');
|
|
76
|
+
await wait(2000);
|
|
77
|
+
console.log('Done after 2 seconds!');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
example();
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Browser (via CDN)
|
|
84
|
+
|
|
85
|
+
```html
|
|
86
|
+
<script type="module">
|
|
87
|
+
// Using unpkg
|
|
88
|
+
import wait from 'https://unpkg.com/waiit/app.js';
|
|
89
|
+
|
|
90
|
+
await wait(1000);
|
|
91
|
+
console.log('Waited 1 second!');
|
|
92
|
+
</script>
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Practical Examples
|
|
96
|
+
|
|
97
|
+
#### Retry with delay
|
|
98
|
+
|
|
99
|
+
```js
|
|
100
|
+
const wait = require('waiit');
|
|
101
|
+
|
|
102
|
+
async function fetchWithRetry(url, retries = 3, delay = 1000) {
|
|
103
|
+
for (let i = 0; i < retries; i++) {
|
|
104
|
+
try {
|
|
105
|
+
return await fetch(url);
|
|
106
|
+
} catch (error) {
|
|
107
|
+
if (i < retries - 1) {
|
|
108
|
+
await wait(delay);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
throw new Error('Max retries reached');
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
#### Sequential execution with delays
|
|
117
|
+
|
|
118
|
+
```js
|
|
119
|
+
const wait = require('waiit');
|
|
120
|
+
|
|
121
|
+
async function animateSteps(steps) {
|
|
122
|
+
for (const step of steps) {
|
|
123
|
+
console.log(step.message);
|
|
124
|
+
await wait(step.delay);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
animateSteps([
|
|
129
|
+
{ message: 'Step 1', delay: 1000 },
|
|
130
|
+
{ message: 'Step 2', delay: 2000 },
|
|
131
|
+
{ message: 'Step 3', delay: 1000 },
|
|
132
|
+
]);
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## License
|
|
136
|
+
|
|
137
|
+
[MIT](LICENSE) © Raju Dhami
|
package/app.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports=t=>new Promise(r=>setTimeout(r,t));
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wait for a specified amount of time.
|
|
3
|
+
* @param ms - Time to wait in milliseconds. Defaults to 0 (resolves immediately).
|
|
4
|
+
* @returns A promise that resolves after the specified time.
|
|
5
|
+
* @example
|
|
6
|
+
* await wait(2000); // Wait for 2 seconds
|
|
7
|
+
* await wait(); // Resolve immediately
|
|
8
|
+
*/
|
|
9
|
+
declare function wait(ms?: number): Promise<void>;
|
|
10
|
+
export = wait;
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "waiit",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Wait for any amount of time in milliseconds or resolve immediately if no value passed",
|
|
5
|
+
"main": "app.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"require": "./app.js",
|
|
10
|
+
"import": "./app.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"test": "node test.js && node test.mjs",
|
|
15
|
+
"test:cjs": "node test.js",
|
|
16
|
+
"test:esm": "node test.mjs"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/Raju/waiit.git"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"wait",
|
|
24
|
+
"delay",
|
|
25
|
+
"sleep",
|
|
26
|
+
"timeout",
|
|
27
|
+
"promise",
|
|
28
|
+
"async",
|
|
29
|
+
"await",
|
|
30
|
+
"setTimeout",
|
|
31
|
+
"timer",
|
|
32
|
+
"pause",
|
|
33
|
+
"browser",
|
|
34
|
+
"node"
|
|
35
|
+
],
|
|
36
|
+
"author": "Raju Dhami",
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/Raju/waiit/issues"
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://github.com/Raju/waiit#readme",
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=10.0.0"
|
|
44
|
+
},
|
|
45
|
+
"files": [
|
|
46
|
+
"app.js",
|
|
47
|
+
"index.d.ts"
|
|
48
|
+
]
|
|
49
|
+
}
|