MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1// Test that the this stack is properly managed across async boundaries
2
3class StackTest {
4 constructor(name) {
5 this.name = name;
6 }
7
8 // Regular method that calls another with function arg
9 methodA(getArg) {
10 console.log('methodA: this.name = ' + this.name);
11 return this.methodB(getArg());
12 }
13
14 methodB(arg) {
15 console.log('methodB: this.name = ' + this.name + ', arg = ' + arg);
16 return this.name + ':' + arg;
17 }
18
19 // Method that returns a promise
20 asyncMethod() {
21 console.log('asyncMethod: this.name = ' + this.name);
22 return Promise.resolve(this.name);
23 }
24}
25
26function helperFunc() {
27 return 'helper';
28}
29
30const obj1 = new StackTest('obj1');
31const obj2 = new StackTest('obj2');
32
33console.log('=== Test 1: Synchronous nested calls ===');
34const result1 = obj1.methodA(() => helperFunc());
35console.log('Result: ' + result1);
36
37console.log('\n=== Test 2: Async then sync ===');
38obj1.asyncMethod().then((val) => {
39 console.log('In .then(), val = ' + val);
40 // Now do a sync call with function arg
41 const result = obj2.methodA(() => 'fromPromise');
42 console.log('After sync call: ' + result);
43});
44
45console.log('\n=== Test 3: Multiple objects interleaved ===');
46obj1.methodA(() => {
47 obj2.methodB('nested');
48 return 'test';
49});
50
51console.log('\n=== All tests done ===');