Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F2921977
test_async_this_binding.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_this_binding.cjs
View Options
// Test explicit this binding in async contexts
class TestClass {
constructor(name) {
this.name = name;
}
methodThatReturnsPromise() {
console.log('methodThatReturnsPromise: this.name = ' + this.name);
// Use regular function (not arrow) in .then()
return Promise.resolve('value').then(function(val) {
// In standard JS, 'this' would be undefined here
// Let's see what Ant does
console.log('In .then() regular function:');
console.log(' typeof this: ' + typeof this);
console.log(' this: ' + this);
// Try to access properties
if (typeof this.name !== 'undefined') {
console.log(' this.name: ' + this.name);
} else {
console.log(' this.name is undefined');
}
return val;
});
}
}
const obj1 = new TestClass('Object1');
const obj2 = new TestClass('Object2');
console.log('=== Calling obj1.methodThatReturnsPromise() ===');
obj1.methodThatReturnsPromise().then(function(result) {
console.log('Final result: ' + result);
});
console.log('\n=== Calling obj2.methodThatReturnsPromise() ===');
obj2.methodThatReturnsPromise().then(function(result) {
console.log('Final result: ' + result);
});
console.log('\n=== Tests queued ===');
File Metadata
Details
Attached
Mime Type
application/javascript
Expires
Fri, Mar 27, 11:40 AM (1 d, 23 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
512289
Default Alt Text
test_async_this_binding.cjs (1 KB)
Attached To
Mode
rANT Ant
Attached
Detach File
Event Timeline
Log In to Comment