Something went wrong. Try again.
javascript for 🐜's, a tiny runtime with big ambitions antjs.org
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152// Test that the this stack is properly managed across async boundaries
class StackTest { constructor(name) { this.name = name; } // Regular method that calls another with function arg methodA(getArg) { console.log('methodA: this.name = ' + this.name); return this.methodB(getArg()); } methodB(arg) { console.log('methodB: this.name = ' + this.name + ', arg = ' + arg); return this.name + ':' + arg; } // Method that returns a promise asyncMethod() { console.log('asyncMethod: this.name = ' + this.name); return Promise.resolve(this.name); }}
function helperFunc() { return 'helper';}
const obj1 = new StackTest('obj1');const obj2 = new StackTest('obj2');
console.log('=== Test 1: Synchronous nested calls ===');const result1 = obj1.methodA(() => helperFunc());console.log('Result: ' + result1);
console.log('\n=== Test 2: Async then sync ===');obj1.asyncMethod().then((val) => { console.log('In .then(), val = ' + val); // Now do a sync call with function arg const result = obj2.methodA(() => 'fromPromise'); console.log('After sync call: ' + result);});
console.log('\n=== Test 3: Multiple objects interleaved ===');obj1.methodA(() => { obj2.methodB('nested'); return 'test';});
console.log('\n=== All tests done ===');