Proposal: GitHub - wentout/awaited-assign: async set feature
Help me please to convert it to https://github.com/tc39/awaited-assignment
Let consider the following JavaScript code piece we may use on modern engines today:
'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 );
} );
}
} );
This code example above is fully functional and working at least via V8 runtime. Having Async Getters for today we allowed do consder that Setter is also asynchronous, but, unfortunately while being asynchronous indeed we are not allowed to track this operation. So having this code piece below is a scenario when we lack of instrumentation:
( async () => {
console.log( 'initial : ', await myObj.field );
console.log( 'the moment of assignment invocation : ', myObj.field = 321 );
console.log( 'real value during changes happening : ', field );
console.log( 'reading assigned value after change : ', await myObj.field );
console.log( 'real value when changes indeed made : ', field );
} )();
Here we see the moment between assignment operator invocation and real value change today is unpredictable.
Thus let assume we may use the following construct to keep eye on this somehow:
await myObj.field = value;
This means Assignment Operation may have Asynchronous Behaviour for consistency with Async Getter