Zalgo Promise

1.0.48 · active · verified Sun Apr 19

The `zalgo-promise` library (current stable version 1.0.48) is a specialized JavaScript promise implementation designed to deviate from the standard asynchronous-by-default behavior of native `Promise` objects. It resolves synchronously unless an explicit asynchronous operation (e.g., `setTimeout`, network request) is introduced within the promise chain. This unique characteristic makes it particularly useful for mitigating specific performance bottlenecks in browser environments, such as issues with `setTimeout` deprioritization in unfocused popup windows, or when polyfilling Promises in older browsers where shims might rely on inefficient asynchronous mechanisms. `zalgo-promise` provides a more direct control over execution flow compared to standard promises, making it a niche but valuable tool for developers facing these particular challenges. While it doesn't have a rapid release cadence, it is actively maintained for its specific use case.

Common errors

Warnings

Install

Imports

Quickstart

This example showcases `zalgo-promise`'s core behavior: synchronous resolution for immediate values and asynchronous resolution when a `setTimeout` (or similar async operation) is introduced.

import { ZalgoPromise } from 'zalgo-promise';

// Demonstrates synchronous resolution by default
const syncPromise = new ZalgoPromise(function(resolve) {
    console.log('Creating synchronous promise...');
    resolve('Synchronous result');
});

syncPromise.then(function(result) {
    console.log(`Handler 1: ${result}`); // This logs synchronously
});

console.log('Code after synchronous promise handler.');

// Demonstrates asynchronous resolution when an async operation is involved
const asyncPromise = new ZalgoPromise(function(resolve) {
    console.log('Creating asynchronous promise...');
    setTimeout(() => {
        resolve('Asynchronous result');
    }, 10);
});

asyncPromise.then(function(result) {
    console.log(`Handler 2: ${result}`); // This logs asynchronously after ~10ms
});

console.log('Code after asynchronous promise creation.');

view raw JSON →