A lightweight, environment-agnostic, production-ready Promise utility library for managing concurrent Promise executions and their side effects.
- 🌐 Last Promise Resolution - Ensures only the most recent Promise affects your application state
- 🏃♂️ Race Promise Resolution - Acts on the first resolved Promise while managing others
- 🕒 Timed Promises - Create Promises that resolve after a specified timeout
- 🔄 Promise Wrapper - Wraps synchronous or asynchronous functions into Promises
- 🏗️ Zero Dependencies - Lightweight and focused functionality
- 🔒 Type-Safe - Written in TypeScript with full type definitions
- 🧪 Well Tested - Comprehensive test coverage
# Using npm
npm install prutill
# Using yarn
yarn add prutill
# Using pnpm
pnpm add prutill
import {
getLastPromise,
getRaceWonPromise,
TimedPromise,
} from "https://raw.githubusercontent.com/dominikj111/prutill/main/mod.ts";
- ✅ Node.js (CommonJS)
- ✅ ES Modules
- ✅ Deno
- ✅ Browsers
Useful for scenarios where you only want to act on the most recent Promise, like in React's useEffect:
import { getLastPromise } from "prutill";
// React example
useEffect(() => {
getLastPromise("data-fetch", fetchData()).then(data => {
// Only the latest fetch will update the state
setState(data);
});
}, [dependency1, dependency2]);
When you want to act on the first resolved Promise:
import { getRaceWonPromise } from "prutill";
// Multiple API endpoints example
getRaceWonPromise("fastest-api", fetch("api1")).then(data => {
// First resolved API response wins
processData(data);
});
getRaceWonPromise("fastest-api", fetch("api2"));
Create Promises that resolve after a specific duration:
import { TimedPromise } from "prutill";
// Resolve after 500ms
new TimedPromise(500).then(() => {
console.log("500ms passed");
});
// Resolve with value after 1000ms
new TimedPromise(1000, "Hello").then(value => {
console.log(value); // Outputs: "Hello"
});
Wrap any synchronous or asynchronous function into a Promise:
import { promiseWrapper } from "prutill";
// Wrap a synchronous function
const result1 = await promiseWrapper(() => 42);
console.log(result1); // 42
// Wrap an asynchronous function
const result2 = await promiseWrapper(async () => {
const response = await fetch("https://api.example.com/data");
return response.json();
});
// Error handling
try {
await promiseWrapper(() => {
throw new Error("Something went wrong");
});
} catch (error) {
console.error(error); // Error: Something went wrong
}
key
: Unique identifier for the promise stackpromise
: Promise to add to the stackresolveAllPrevious
: If true, resolves all previous promises with the latest value
key
: Unique identifier for the promise racepromise
: Promise to add to the raceresolveAllOthers
: If true, resolves all other promises with the winning value
constructor(timeout: number, passThrough?: T)
timeout
: Time in milliseconds before the promise resolvespassThrough
: Optional value to resolve with
We welcome contributions! Please see our Contributing Guide for details.
Apache-2.0 © dominikj111
This library is licensed under the Apache License, Version 2.0. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0