Something went wrong. Try again.
javascript for 🐜's, a tiny runtime with big ambitions antjs.org
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344const assert = require('node:assert');
class AsyncRegularFunctionTest { constructor(name) { this.name = name; this.value = 100; }
withRegularFunction() { return Promise.resolve(42).then(function (val) { return { self: this, val: val + 1 }; }); }
withArrowFunction() { return Promise.resolve(42).then((val) => ({ self: this, val: val + 1 })); }
withCapturedSelf() { const self = this; return Promise.resolve(1).then(function () { return { self: this, value: self.value }; }); }}
const obj = new AsyncRegularFunctionTest('TestObject');
(async () => { const regular = await obj.withRegularFunction(); assert.strictEqual(regular.self, undefined); assert.strictEqual(regular.val, 43);
const arrow = await obj.withArrowFunction(); assert.strictEqual(arrow.self, obj); assert.strictEqual(arrow.val, 43);
const captured = await obj.withCapturedSelf(); assert.strictEqual(captured.self, undefined); assert.strictEqual(captured.value, 100);
console.log('async regular function this ok');})();