A lightweight library that provides tools for organizing asynchronous code
—
Core Promise implementation following Promises/A+ specification with additional utility methods and ES6 compliance.
Creates a new Promise instance with a resolver function.
/**
* Promise constructor following Promises/A+ specification
* @param resolver - Function with (resolve, reject) parameters
* @param label - Optional string for debugging/tooling
*/
class Promise {
constructor(resolver: Function, label?: string);
}Usage Examples:
import { Promise } from "rsvp";
// Basic promise creation
const promise = new Promise(function(resolve, reject) {
setTimeout(() => resolve("Hello World"), 1000);
});
// Promise with error handling
const riskyPromise = new Promise(function(resolve, reject) {
if (Math.random() > 0.5) {
resolve("Success!");
} else {
reject(new Error("Something went wrong"));
}
});The primary way of interacting with a promise through callback registration.
/**
* Register callbacks for promise fulfillment and rejection
* @param onFulfillment - Called when promise fulfills
* @param onRejection - Called when promise rejects
* @param label - Optional string for debugging/tooling
* @returns New promise for chaining
*/
then(onFulfillment?: Function, onRejection?: Function, label?: string): Promise;Usage Examples:
promise.then(function(value) {
console.log("Fulfilled:", value);
}, function(reason) {
console.log("Rejected:", reason);
});
// Chaining
promise
.then(value => value.toUpperCase())
.then(upperValue => upperValue + "!")
.then(result => console.log(result));Sugar for handling promise rejections.
/**
* Register callback for promise rejection only
* @param onRejection - Called when promise rejects
* @param label - Optional string for debugging/tooling
* @returns New promise for chaining
*/
catch(onRejection: Function, label?: string): Promise;Usage Examples:
promise
.then(value => processValue(value))
.catch(error => {
console.error("Error occurred:", error);
return "Default value";
});Execute callback regardless of promise outcome.
/**
* Execute callback when promise settles (fulfills or rejects)
* @param callback - Function to execute regardless of outcome
* @param label - Optional string for debugging/tooling
* @returns New promise maintaining original value/reason
*/
finally(callback: Function, label?: string): Promise;Usage Examples:
fetchData()
.then(data => processData(data))
.catch(error => handleError(error))
.finally(() => {
console.log("Operation completed");
hideLoadingSpinner();
});Wait for all promises to fulfill or any to reject.
/**
* Wait for all promises in array to fulfill
* @param array - Array of promises or values
* @param label - Optional string for debugging/tooling
* @returns Promise that fulfills with array of results
*/
static all(array: Array, label?: string): Promise;Race multiple promises, settling with the first to settle.
/**
* Race promises, settling with first to settle
* @param array - Array of promises or values
* @param label - Optional string for debugging/tooling
* @returns Promise that settles with first settled value/reason
*/
static race(array: Array, label?: string): Promise;Create a fulfilled promise with the given value.
/**
* Create promise resolved with given value
* @param value - Value to resolve promise with
* @param label - Optional string for debugging/tooling
* @returns Promise resolved with value
*/
static resolve(value?: any, label?: string): Promise;Create a rejected promise with the given reason.
/**
* Create promise rejected with given reason
* @param reason - Reason for rejection
* @param label - Optional string for debugging/tooling
* @returns Promise rejected with reason
*/
static reject(reason?: any, label?: string): Promise;Deprecated alias for Promise.resolve, maintained for backwards compatibility.
/**
* @deprecated Use Promise.resolve() instead
* Create promise resolved with given value
* @param value - Value to resolve promise with
* @param label - Optional string for debugging/tooling
* @returns Promise resolved with value
*/
static cast(value?: any, label?: string): Promise;Usage Examples for Static Methods:
import { Promise } from "rsvp";
// Promise.all
Promise.all([
fetchUser(1),
fetchUser(2),
fetchUser(3)
]).then(users => {
console.log("All users:", users);
});
// Promise.race with timeout
Promise.race([
fetchData(),
new Promise((_, reject) =>
setTimeout(() => reject(new Error("Timeout")), 5000)
)
]).then(result => {
console.log("Got result:", result);
});
// Promise.resolve for immediate values
Promise.resolve("immediate value")
.then(value => console.log(value));
// Promise.reject for immediate errors
Promise.reject(new Error("Something failed"))
.catch(error => console.error(error));
// Promise.cast (deprecated - use Promise.resolve instead)
Promise.cast("immediate value")
.then(value => console.log(value)); // Not recommended for new codeInstall with Tessl CLI
npx tessl i tessl/npm-rsvp