Bare bones Promises/A+ implementation with essential extensions for readable, performant asynchronous operation handling.
npx @tessl/cli install tessl/npm-promise@8.1.0Promise is a bare bones implementation of the Promises/A+ specification designed for readable, performant code with minimal but essential extensions. It provides foundational promise functionality for JavaScript applications requiring reliable asynchronous operation handling with maximum compatibility and performance.
npm install promiseconst Promise = require('promise');For specific execution environments:
// For Node.js domains support
const Promise = require('promise/domains');
// For setImmediate optimization
const Promise = require('promise/setimmediate');
// ES6-only features
const Promise = require('promise/lib/es6-extensions');
// or require('promise/domains/es6-extensions');
// or require('promise/setimmediate/es6-extensions');
// Core only (deprecated entry point)
const Promise = require('promise/core'); // Use promise/lib/core insteadBrowser polyfill:
<script src="https://www.promisejs.org/polyfills/promise-6.1.0.js"></script>const Promise = require('promise');
// Create a new promise
const promise = new Promise(function (resolve, reject) {
// Async operation
setTimeout(() => {
if (Math.random() > 0.5) {
resolve('Success!');
} else {
reject(new Error('Failed'));
}
}, 1000);
});
// Handle the promise
promise
.then(result => console.log(result))
.catch(error => console.error(error));Promise implements the complete Promises/A+ specification with several key architectural decisions:
asap library for optimal async schedulingFoundational Promise constructor and chaining methods following the Promises/A+ specification.
function Promise(executor);
Promise.prototype.then(onFulfilled, onRejected);Standard ES6 Promise static methods for creating and combining promises.
Promise.resolve(value);
Promise.reject(reason);
Promise.all(iterable);
Promise.allSettled(iterable);
Promise.race(iterable);
Promise.any(iterable);Additional instance methods that extend the core Promise functionality.
Promise.prototype.catch(onRejected);
Promise.prototype.done(onFulfilled, onRejected);
Promise.prototype.finally(onFinally);Utilities for bridging callback-based APIs with promises in Node.js environments.
Promise.denodeify(fn, argumentCount);
Promise.nodeify(fn);
Promise.prototype.nodeify(callback, ctx);Optional methods for synchronously inspecting promise state (disabled by default).
Promise.enableSynchronous();
Promise.disableSynchronous();Development utilities for tracking and debugging unhandled promise rejections.
const rejectionTracking = require('promise/lib/rejection-tracking');
rejectionTracking.enable(options);
rejectionTracking.disable();/**
* Promise constructor following Promises/A+ specification
* @param {function} executor - Function with resolve and reject parameters
*/
function Promise(executor);
/**
* Promise state values (internal)
* 0 - pending
* 1 - fulfilled
* 2 - rejected
* 3 - adopted (follows another promise)
*/
/**
* Result interface for fulfilled promises in allSettled
* @typedef {Object} PromiseFulfilledResult
* @property {"fulfilled"} status - Status indicator
* @property {*} value - The fulfillment value
*/
/**
* Result interface for rejected promises in allSettled
* @typedef {Object} PromiseRejectedResult
* @property {"rejected"} status - Status indicator
* @property {*} reason - The rejection reason
*/
/**
* Union type for promise settlement results
* @typedef {PromiseFulfilledResult|PromiseRejectedResult} PromiseSettledResult
*/