Page MenuHomePhorge

test_async_regular_function.cjs
No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None

test_async_regular_function.cjs

// Test that 'this' works with regular functions in async callbacks
class AsyncRegularFunctionTest {
constructor(name) {
this.name = name;
this.value = 100;
}
testPromiseWithRegularFunction() {
console.log('=== testPromiseWithRegularFunction ===');
console.log('Before promise: this.name = ' + this.name);
return Promise.resolve(42).then(function(val) {
console.log('Inside .then() with function:');
console.log(' typeof this: ' + typeof this);
console.log(' this.name: ' + this.name);
console.log(' this.value: ' + this.value);
return val + 1;
});
}
testPromiseWithArrowFunction() {
console.log('\n=== testPromiseWithArrowFunction ===');
console.log('Before promise: this.name = ' + this.name);
return Promise.resolve(42).then((val) => {
console.log('Inside .then() with arrow:');
console.log(' typeof this: ' + typeof this);
console.log(' this.name: ' + this.name);
console.log(' this.value: ' + this.value);
return val + 1;
});
}
testMethodCallingPromiseWithFunction() {
console.log('\n=== testMethodCallingPromiseWithFunction ===');
const self = this;
console.log('Before promise: this.name = ' + this.name);
return Promise.resolve(1).then(function() {
console.log('In .then() with function:');
console.log(' this.name: ' + this.name);
console.log(' self.name: ' + self.name);
return self.value;
});
}
}
const obj = new AsyncRegularFunctionTest('TestObject');
obj.testPromiseWithRegularFunction().then(function(result) {
console.log('Result 1: ' + result);
});
obj.testPromiseWithArrowFunction().then((result) => {
console.log('Result 2: ' + result);
});
obj.testMethodCallingPromiseWithFunction().then((result) => {
console.log('Result 3: ' + result);
});
console.log('\n=== All tests queued ===');

File Metadata

Mime Type
application/javascript
Expires
Fri, Mar 27, 6:15 AM (2 d)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
512254
Default Alt Text
test_async_regular_function.cjs (1 KB)

Event Timeline