Bare bones Promises/A+ implementation with essential extensions for readable, performant asynchronous operation handling.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Core Promise implementation following the Promises/A+ specification with optimized performance and reliability.
Creates a new Promise instance with an executor function.
/**
* Creates a new Promise instance
* @param {function} executor - Function called with (resolve, reject) parameters
* @throws {TypeError} If not called with new or executor is not a function
*/
function Promise(executor);Usage Examples:
const Promise = require('promise');
// Basic promise creation
const promise = new Promise((resolve, reject) => {
// Simulate async operation
setTimeout(() => {
if (Math.random() > 0.5) {
resolve('Operation successful');
} else {
reject(new Error('Operation failed'));
}
}, 1000);
});
// Immediate resolution
const resolved = new Promise((resolve) => {
resolve(42);
});
// Immediate rejection
const rejected = new Promise((resolve, reject) => {
reject(new Error('Something went wrong'));
});Attaches callbacks for promise resolution and/or rejection, returning a new promise.
/**
* Attaches fulfillment and/or rejection handlers
* @param {function} [onFulfilled] - Called when promise is fulfilled
* @param {function} [onRejected] - Called when promise is rejected
* @returns {Promise} New promise for the result
*/
Promise.prototype.then(onFulfilled, onRejected);Usage Examples:
const Promise = require('promise');
// Basic chaining
promise
.then(result => {
console.log('Success:', result);
return result * 2;
})
.then(doubled => {
console.log('Doubled:', doubled);
});
// Error handling with then
promise.then(
result => console.log('Success:', result),
error => console.error('Error:', error)
);
// Transformation
promise
.then(data => JSON.parse(data))
.then(parsed => parsed.users)
.then(users => users.filter(u => u.active));/**
* Internal static properties (do not use directly)
*/
Promise._onHandle; // Internal handler for promise events
Promise._onReject; // Internal handler for rejection events
Promise._noop; // Internal no-operation functionThe core Promise implementation includes sophisticated error handling:
Example:
// Synchronous errors are caught automatically
const promise = new Promise((resolve, reject) => {
throw new Error('Synchronous error'); // Automatically becomes rejection
});
promise.catch(error => {
console.log('Caught:', error.message); // "Synchronous error"
});Install with Tessl CLI
npx tessl i tessl/npm-promise