Install and import
easy-stack has no runtime dependencies. Node.js 22.13 or newer can use native ESM or CommonJS; both loaders resolve the same synchronous stack.js implementation.
npm install easy-stack// Native ESM
import Stack from 'easy-stack';
// CommonJS
const Stack = require('easy-stack');Both imports return the same constructor identity. The package uses Node's native ESM/CommonJS interoperability without a compiler, bundler, or duplicate runtime file.
easy-stack works with bundlers and without a bundler. Bundlers resolve easy-stack normally; native browser ESM uses an import map before the module script, with no build or transpilation step. The browser guide shows the complete normal npm-layout map and HTTP(S) serving requirements.
Run a first stack
Disable auto-run while loading the example so the complete batch is present before execution starts. The last callback added executes first.
import Stack from 'easy-stack';
const stack = new Stack();
const order = [];
stack.autoRun = false;
stack.add(
function first() {
order.push('first');
this.next();
},
function second() {
order.push('second');
this.next();
},
function third() {
order.push('third');
this.next();
}
);
stack.next();
console.log(order); // ['third', 'second', 'first']easy-stack calls each task with the stack as this. Arrow functions keep their lexical this and therefore cannot call this.next().
Understand the cooperative model
Auto-run controls only the initial start. Once a task is active, the stack waits until that task calls next(). This makes a task a deliberate hand-off point for synchronous work, timers, events, or network responses.
add()places every supplied function on top in argument order.- The last supplied function is selected first.
- A task may pause simply by returning without calling
next(). - Resume later by calling
stack.next()from the appropriate completion signal. - Set
stop = trueto gate selection without discarding pending functions.