CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-promise

Bare bones Promises/A+ implementation with essential extensions for readable, performant asynchronous operation handling.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

core-promise.mddocs/

Core Promise

Core Promise implementation following the Promises/A+ specification with optimized performance and reliability.

Capabilities

Promise Constructor

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'));
});

Then Method

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 Properties

/**
 * 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 function

Error Handling

The core Promise implementation includes sophisticated error handling:

  • Synchronous errors in the executor are automatically caught and convert the promise to rejected
  • Type checking ensures proper usage of constructor and methods
  • State immutability prevents multiple resolution/rejection of the same promise
  • Exception isolation prevents errors from escaping the promise chain

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

docs

core-promise.md

es6-static-methods.md

extension-methods.md

index.md

nodejs-integration.md

rejection-tracking.md

synchronous-inspection.md

tile.json