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

es6-static-methods.mddocs/

ES6 Static Methods

Standard ES6 Promise static methods for creating resolved/rejected promises and combining multiple promises.

Capabilities

Promise.resolve

Creates a promise resolved with the given value, with optimization for common values.

/**
 * Creates a resolved promise with the given value
 * @param {*} [value] - Value to resolve with (can be a promise or thenable)
 * @returns {Promise} Promise resolved with the value
 */
Promise.resolve(value);

Usage Examples:

const Promise = require('promise');

// Resolve with a value
const resolved = Promise.resolve(42);

// Resolve with no value (undefined)
const empty = Promise.resolve();

// Resolve with another promise (returns the same promise)
const original = Promise.resolve('hello');
const same = Promise.resolve(original); // same === original

// Resolve with a thenable
const thenable = {
  then: (resolve, reject) => resolve('from thenable')
};
const fromThenable = Promise.resolve(thenable);

Promise.reject

Creates a promise rejected with the given reason.

/**
 * Creates a rejected promise with the given reason
 * @param {*} reason - Reason for rejection
 * @returns {Promise} Promise rejected with the reason
 */
Promise.reject(reason);

Usage Examples:

const Promise = require('promise');

// Reject with an error
const rejected = Promise.reject(new Error('Something failed'));

// Reject with any value
const rejectedWithString = Promise.reject('Operation cancelled');

// Use in promise chains
Promise.resolve(data)
  .then(result => {
    if (!result.isValid) {
      return Promise.reject(new Error('Invalid data'));
    }
    return result.value;
  });

Promise.all

Returns a promise that resolves when all input promises resolve, or rejects when any input promise rejects.

/**
 * Waits for all promises to resolve
 * @param {Iterable} iterable - Array or iterable of promises/values
 * @returns {Promise} Promise resolving to array of results in same order
 */
Promise.all(iterable);

Usage Examples:

const Promise = require('promise');

// Wait for multiple promises
const promises = [
  Promise.resolve(1),
  Promise.resolve(2),
  Promise.resolve(3)
];

Promise.all(promises).then(results => {
  console.log(results); // [1, 2, 3]
});

// Mix promises and values
Promise.all([
  fetch('/api/users'),
  fetch('/api/posts'), 
  42,
  'hello'
]).then(results => {
  const [users, posts, number, string] = results;
  // Process results...
});

// Fails fast on first rejection
Promise.all([
  Promise.resolve(1),
  Promise.reject(new Error('Failed')),
  Promise.resolve(3)
]).catch(error => {
  console.log(error.message); // "Failed"
});

Promise.allSettled

Returns a promise that resolves when all input promises settle (resolve or reject), with results indicating the outcome of each.

/**
 * Waits for all promises to settle regardless of outcome
 * @param {Iterable} iterable - Array or iterable of promises/values
 * @returns {Promise} Promise resolving to array of settlement results
 */
Promise.allSettled(iterable);

Usage Examples:

const Promise = require('promise');

const promises = [
  Promise.resolve('success'),
  Promise.reject(new Error('failed')),
  Promise.resolve(42)
];

Promise.allSettled(promises).then(results => {
  console.log(results);
  // [
  //   { status: 'fulfilled', value: 'success' },
  //   { status: 'rejected', reason: Error('failed') },
  //   { status: 'fulfilled', value: 42 }
  // ]
  
  results.forEach((result, index) => {
    if (result.status === 'fulfilled') {
      console.log(`Promise ${index} succeeded:`, result.value);
    } else {
      console.log(`Promise ${index} failed:`, result.reason);
    }
  });
});

Promise.race

Returns a promise that settles as soon as the first input promise settles.

/**
 * Returns the result of the first promise to settle
 * @param {Iterable} iterable - Array or iterable of promises/values  
 * @returns {Promise} Promise settling with first settlement result
 */
Promise.race(iterable);

Usage Examples:

const Promise = require('promise');

// Timeout pattern
const timeout = new Promise((_, reject) => {
  setTimeout(() => reject(new Error('Timeout')), 5000);
});

const operation = fetch('/api/data');

Promise.race([operation, timeout])
  .then(result => console.log('Completed within timeout'))
  .catch(error => console.log('Failed or timed out:', error.message));

// First to complete wins
Promise.race([
  new Promise(resolve => setTimeout(() => resolve('slow'), 1000)),
  new Promise(resolve => setTimeout(() => resolve('fast'), 100))
]).then(result => {
  console.log(result); // "fast"
});

Promise.any

Returns a promise that fulfills with the first input promise to fulfill, or rejects with an AggregateError if all input promises reject.

/**
 * Returns the result of the first promise to fulfill
 * @param {Iterable} iterable - Array or iterable of promises/values
 * @returns {Promise} Promise fulfilling with first fulfillment or AggregateError
 */
Promise.any(iterable);

Usage Examples:

const Promise = require('promise');

// First successful result
const mirrors = [
  fetch('https://api1.example.com/data'),
  fetch('https://api2.example.com/data'),
  fetch('https://api3.example.com/data')
];

Promise.any(mirrors)
  .then(response => console.log('Got response from fastest server'))
  .catch(error => {
    // AggregateError if all fail
    console.log('All mirrors failed:', error.errors);
  });

// Mixed success and failure
Promise.any([
  Promise.reject(new Error('Failed 1')),
  Promise.resolve('Success!'),
  Promise.reject(new Error('Failed 2'))
]).then(result => {
  console.log(result); // "Success!"
});

// All failures
Promise.any([
  Promise.reject(new Error('Error 1')),
  Promise.reject(new Error('Error 2'))
]).catch(error => {
  console.log(error.name); // "AggregateError"
  console.log(error.errors); // [Error('Error 1'), Error('Error 2')]
});

Types

/**
 * Settlement result for Promise.allSettled
 */
interface SettlementResult {
  status: 'fulfilled' | 'rejected';
  value?: any;     // Present when status is 'fulfilled'
  reason?: any;    // Present when status is 'rejected'
}

/**
 * Error thrown by Promise.any when all promises reject
 */
interface AggregateError extends Error {
  name: 'AggregateError';
  errors: any[];   // Array of rejection reasons
}

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