Zalgo Promise
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
-
ReferenceError: ZalgoPromise is not defined
cause The `ZalgoPromise` symbol was not correctly imported or made available in the current scope.fixFor ES modules, use `import { ZalgoPromise } from 'zalgo-promise';`. For CommonJS, `const ZalgoPromise = require('zalgo-promise');`. If using a global script tag, ensure `zalgo-promise.js` is loaded before your code executes. -
My promise chain executes immediately, blocking the UI, unlike native Promises.
cause This is the intended behavior of `zalgo-promise` for synchronously resolvable values. It bypasses the microtask queue that native Promises use.fixIf you need asynchronous, non-blocking behavior (similar to native Promises), you must explicitly introduce an asynchronous operation within your `ZalgoPromise` chain, such as `setTimeout` or a network request. -
I'm seeing different execution orders when mixing ZalgoPromise with native Promises.
cause The synchronous-by-default nature of `ZalgoPromise` conflicts with the always-asynchronous nature of native Promises (even `Promise.resolve()` is async via microtasks).fixEnsure clear boundaries between code sections using `ZalgoPromise` and those using native Promises. Avoid implicitly relying on resolution order when intermixing them. Convert `ZalgoPromise` to a native Promise (e.g., `Promise.resolve(new ZalgoPromise(...))`) if consistency with native behavior is required.
Warnings
- breaking ZalgoPromise fundamentally changes the expected asynchronous behavior of standard JavaScript Promises. Operations that would be asynchronous (via microtask queue) with native `Promise.resolve().then()` will execute synchronously with `ZalgoPromise` unless an explicit asynchronous action is involved.
- gotcha Mixing `zalgo-promise` with native Promises or other promise polyfills can lead to unpredictable execution order due to their differing resolution models. Operations might complete in an order unexpected by developers accustomed to standard Promise behavior.
- gotcha While designed to prevent hangs in certain browser scenarios, over-reliance on synchronous promise resolution for CPU-intensive tasks can block the main thread, leading to a frozen UI. The benefits of asynchronicity for non-blocking operations are bypassed.
- gotcha The name 'Zalgo' references an internet meme for 'creepy text' or 'coming from beyond the screen,' implying uncontrolled or unexpected behavior. While humorous, it's a reminder that this library intentionally breaks standard Promise expectations, which can be a footgun if not fully understood.
Install
-
npm install zalgo-promise -
yarn add zalgo-promise -
pnpm add zalgo-promise
Imports
- ZalgoPromise
import ZalgoPromise from 'zalgo-promise'; // Or for CommonJS in ESM context: const ZalgoPromise = require('zalgo-promise');import { ZalgoPromise } from 'zalgo-promise'; - ZalgoPromise (CommonJS)
const ZalgoPromise = require('zalgo-promise'); - ZalgoPromise (Global)
<script src="zalgo-promise.js"></script> <script> new ZalgoPromise( ... ); </script>
Quickstart
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.');