-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changelog: feature
- Loading branch information
Showing
11 changed files
with
120 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { delay } from "@mrspartak/promises" | ||
import { parsePage } from "./parser" | ||
|
||
for (let i = 0; i < 10; i++) { | ||
// Parse the page and wait for 1 second before parsing the next page | ||
const pageData = await parsePage(i) | ||
await delay(1000) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Pauses execution for a specified number of milliseconds. | ||
* | ||
* @param {number} ms - The number of milliseconds to delay execution. | ||
* @returns {Promise<void>} A promise that resolves after the specified delay. | ||
* @throws {Error} Thrown if the input is not a number or if the number is negative. | ||
* | ||
* @includeExample examples/delay.ts | ||
*/ | ||
export async function delay(ms: number): Promise<void> { | ||
// Validate that the input is a number | ||
if (typeof ms !== 'number') { | ||
throw new Error('delay(ms) requires a number as a parameter'); | ||
} | ||
|
||
// Validate that the number is positive | ||
if (ms < 0) { | ||
throw new Error('delay(ms) requires a positive number as a parameter'); | ||
} | ||
|
||
// Return a promise that resolves after the specified delay | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} | ||
|
||
// Alias for delay: sleep | ||
export const sleep = delay; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { to } from "./to"; | ||
import { delay, sleep } from "./delay"; | ||
|
||
export { to }; | ||
export { to, delay, sleep }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from '../index' | ||
export * from '../index' | ||
export * from './web-types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/// <reference types="web" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { delay, sleep } from '../../dist/index.js' | ||
|
||
describe("delay", () => { | ||
it("must be a function", () => { | ||
expect(typeof delay).toBe("function"); | ||
}); | ||
}); | ||
|
||
describe("sleep", () => { | ||
it("must be a function", () => { | ||
expect(typeof sleep).toBe("function"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { delay, sleep } from '../../src/delay.js' | ||
|
||
describe('delay', () => { | ||
it('should be a function', () => { | ||
expect(typeof delay).toBe('function') | ||
}) | ||
|
||
it('should return a promise', () => { | ||
expect(delay(1) instanceof Promise).toBe(true) | ||
}) | ||
|
||
it('should resolve after the specified delay', async () => { | ||
const start = Date.now() | ||
await delay(100) | ||
const end = Date.now() | ||
expect(end - start).toBeGreaterThanOrEqual(100) | ||
}) | ||
|
||
it('should throw an error if the input is not a number', async () => { | ||
// @ts-ignore | ||
await expect(delay('test')).rejects.toThrow('delay(ms) requires a number as a parameter') | ||
}) | ||
|
||
it('should throw an error if the number is negative', async () => { | ||
await expect(delay(-1)).rejects.toThrow('delay(ms) requires a positive number as a parameter') | ||
}) | ||
|
||
it('should not throw an error if the number is zero', async () => { | ||
await expect(delay(0)).resolves.toBeUndefined() | ||
}) | ||
}) | ||
|
||
describe('sleep', () => { | ||
it('should be a function', () => { | ||
expect(typeof sleep).toBe('function') | ||
}) | ||
|
||
it('should return a promise', () => { | ||
expect(sleep(1) instanceof Promise).toBe(true) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters