A lightweight TypeScript utility to create a Promise that resolves after a specified delay. Ideal for async delays in Node.js and browsers.
- Lightweight: Minimal code footprint, easy to integrate.
- TypeScript Support: Built with TypeScript for better type safety.
- Cross-Platform: Works seamlessly in both Node.js and browser environments.
- Promise-Based: Utilizes Promises for clean asynchronous code.
To install the utility, you can use npm or yarn. Run one of the following commands in your terminal:
npm install timeout-typescript
or
yarn add timeout-typescript
To use the utility, simply import it into your TypeScript or JavaScript file. Here’s a basic example:
import { timeout } from 'timeout-typescript';
async function delayedExecution() {
console.log('Waiting for 3 seconds...');
await timeout(3000); // Wait for 3 seconds
console.log('3 seconds have passed!');
}
delayedExecution();
-
Parameters:
ms
: The delay in milliseconds before the Promise resolves.
-
Returns: A Promise that resolves after the specified delay.
timeout(1000).then(() => {
console.log('Executed after 1 second');
});
Here are some practical examples of how to use timeout-typescript
.
import { timeout } from 'timeout-typescript';
async function simpleDelay() {
console.log('Starting delay...');
await timeout(2000); // 2 seconds
console.log('Delay finished!');
}
simpleDelay();
import { timeout } from 'timeout-typescript';
async function chainPromises() {
console.log('First action');
await timeout(1000);
console.log('Second action after 1 second');
await timeout(2000);
console.log('Third action after another 2 seconds');
}
chainPromises();
import { timeout } from 'timeout-typescript';
async function loopWithDelay() {
for (let i = 1; i <= 5; i++) {
console.log(`Iteration ${i}`);
await timeout(1000); // 1 second delay
}
}
loopWithDelay();
We welcome contributions! If you would like to contribute, please follow these steps:
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch
). - Make your changes.
- Commit your changes (
git commit -m 'Add some feature'
). - Push to the branch (
git push origin feature-branch
). - Open a Pull Request.
Please ensure your code follows the existing style and includes appropriate tests.
This project is licensed under the MIT License. See the LICENSE file for details.
For any issues or feature requests, please check the Releases section. You can also reach out via the Issues tab on GitHub.
To download the latest version, visit the Releases page. Here, you can find the latest updates and changes.
- Thanks to the TypeScript community for their contributions and support.
- Special thanks to all the contributors who have helped improve this project.
timeout-typescript
provides a simple yet effective way to manage delays in your asynchronous code. Whether you're building applications for Node.js or the browser, this utility can help streamline your workflows. Explore the examples, and feel free to contribute to make this tool even better!
This README provides a comprehensive overview of the timeout-typescript
utility. For further details, feel free to explore the code and documentation.