A lightweight library that provides tools for organizing asynchronous code
npx @tessl/cli install tessl/npm-rsvp@4.8.0RSVP.js provides simple tools for organizing asynchronous code. It is a lightweight implementation of Promises/A+ specification that works in both Node.js and browsers (IE9+), bringing additional utilities beyond the standard ES6 Promise API.
npm install rsvpimport RSVP from "rsvp";
import { Promise, all, hash, resolve, reject } from "rsvp";For CommonJS:
var RSVP = require("rsvp");
var Promise = RSVP.Promise;For browsers via CDN:
<script src="https://cdn.jsdelivr.net/npm/rsvp@4/dist/rsvp.min.js"></script>import { Promise, all, hash, resolve } from "rsvp";
// Create a promise
const promise = new Promise(function(resolve, reject) {
// async operation
setTimeout(() => resolve("Success!"), 1000);
});
promise.then(function(value) {
console.log(value); // "Success!"
}).catch(function(error) {
console.error(error);
});
// Wait for multiple promises
all([
resolve(1),
resolve(2),
resolve(3)
]).then(function(values) {
console.log(values); // [1, 2, 3]
});
// Work with object of promises
hash({
users: fetchUsers(),
posts: fetchPosts()
}).then(function(results) {
console.log(results.users);
console.log(results.posts);
});RSVP.js is built around several key components:
all, allSettled, race, map, filter)hash, hashSettled)denodeify)Core Promise implementation following Promises/A+ specification with additional methods like finally.
class Promise {
constructor(resolver: Function, label?: string);
then(onFulfillment?: Function, onRejection?: Function, label?: string): Promise;
catch(onRejection: Function, label?: string): Promise;
finally(callback: Function, label?: string): Promise;
static all(array: Array, label?: string): Promise;
static race(array: Array, label?: string): Promise;
static resolve(value?: any, label?: string): Promise;
static reject(reason?: any, label?: string): Promise;
}Functions for working with arrays of promises including parallel execution, racing, transformation, and filtering.
function all(array: Array, label?: string): Promise;
function allSettled(entries: Array, label?: string): Promise;
function race(array: Array, label?: string): Promise;
function map(promises: Array, mapFn: Function, label?: string): Promise;
function filter(promises: Array, filterFn: Function, label?: string): Promise;Functions for working with objects containing promises, useful for handling named async operations.
function hash(object: Object, label?: string): Promise;
function hashSettled(object: Object, label?: string): Promise;Convert Node.js callback-style functions to promise-returning functions for better async composition.
function denodeify(nodeFunc: Function, options?: boolean | Array): Function;Global configuration system and event handling for instrumentation and debugging.
function configure(name: string, value?: any): any;
function on(...arguments): void;
function off(...arguments): void;
const EventTarget: {
mixin(object: Object): Object;
on(eventName: string, callback: Function): void;
off(eventName: string, callback?: Function): void;
trigger(eventName: string, options?: any, label?: string): void;
};Additional utility functions for promise creation, error handling, and async scheduling.
function resolve(value?: any, label?: string): Promise;
function reject(reason?: any, label?: string): Promise;
function defer(label?: string): Object;
function rethrow(reason: any): void;
function asap(callback: Function, arg?: any): void;
function async(callback: Function, arg?: any): void;interface PromiseState {
state: 'fulfilled' | 'rejected';
value?: any;
reason?: any;
}
interface DeferredObject {
promise: Promise;
resolve: Function;
reject: Function;
}