Skip to content

Instantly share code, notes, and snippets.

@wentout
Created April 7, 2025 13:52
Show Gist options
  • Save wentout/aa9c4e8cca11320b304d7757e7db8140 to your computer and use it in GitHub Desktop.
Save wentout/aa9c4e8cca11320b304d7757e7db8140 to your computer and use it in GitHub Desktop.
Awaited Assign
'use strict';
const myObj = {};
let field = 123;
Object.defineProperty( myObj, 'field', {
async get () {
return new Promise( ( resolve, reject ) => {
setTimeout( () => {
resolve( field );
}, 1000 );
} );
},
async set ( value ) {
new Promise( ( resolve, reject ) => {
setTimeout( () => {
field = value;
resolve( field );
}, 100 );
} );
}
} );
( async () => {
console.log( '--- initial awaiting for getter ---', );
console.log( 'initial : ', await myObj.field );
console.time('assignment');
const addition = myObj.field = 321;
console.timeEnd('assignment');
console.log( 'addition : ', addition );
console.time('assignment to field');
console.log( 'the moment of assignment invocation : ', myObj.field = 321 );
console.log( 'real value during changes happening : ', field );
console.timeEnd('assignment to field');
console.log( '--- here we are awaiting for getter ---', );
console.log( 'reading assigned value after change : ', await myObj.field );
console.log( 'real value when changes indeed made : ', field );
} )();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment