Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/franckldx/ts-wait
https://github.com/franckldx/ts-wait
Last synced: 5 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/franckldx/ts-wait
- Owner: franckLdx
- License: mit
- Created: 2024-02-11T17:25:01.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-03-09T15:33:43.000Z (10 months ago)
- Last Synced: 2024-10-30T07:07:29.368Z (about 2 months ago)
- Language: TypeScript
- Size: 67.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ts-wait
I'm tired to write a wait function again and agin in my projects, so I make this simple project.
```javascript
await wait(100) // wait 100 milliseconds
```This lib does not do only this. Mainly it allows to set timetou on function, ethier synchronous or asynchronous:
```javascript
try {
const func = ()=> {/* do something synchronous */},
const result = await waitUntil(
func,
10000 // <- in milliseconds
);
} catch (err) {
if (isTimeoutError(error)) { {
// fn does not complete after 10 seconds
} else {
// fn throws an exception
}
}
```It throws an error of type TimeoutError it timeout occurs. The isTimeoutError is a type guard to check is error is a TimeoutError.
To set a timeout on an async function
```javascript
try {
const result = await waitUntilAsync(async () => {
/* do something */
}, 10000);
} catch (err) {
if (isTimeoutError(error)) {
// fn does not complete after 10 seconds
} else {
// fn throws an exception
}
}
```