Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F2921664
test_async_regular_function.cjs
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
test_async_regular_function.cjs
View Options
// 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
Details
Attached
Mime Type
application/javascript
Expires
Fri, Mar 27, 10:18 AM (1 d, 23 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
512254
Default Alt Text
test_async_regular_function.cjs (1 KB)
Attached To
Mode
rANT Ant
Attached
Detach File
Event Timeline
Log In to Comment