Skip to content

Instantly share code, notes, and snippets.

@wentout
Last active June 22, 2020 07:42
Show Gist options
  • Save wentout/19572ca4f9664ecf08afc71c1a1ddb39 to your computer and use it in GitHub Desktop.
Save wentout/19572ca4f9664ecf08afc71c1a1ddb39 to your computer and use it in GitHub Desktop.
const o = {
note: 'default note'
};
Object.defineProperty(o, 'letter', {
get: async function () {
return this.note;
},
set: async function (value) {
await new Promise((resolve) => {
setTimeout(() => {
o.note = value;
resolve();
}, 100);
});
}
});
Object.defineProperty(o, 'as_async_value', {
async set (value) {
if (value instanceof Function) {
o.note = await value();
return;
}
o.note = value;
}
});
(async function () {
debugger;
console.log('o.letter : ', o.letter);
console.log('await o.letter : ', await o.letter);
o.letter = 123;
console.log('o.note : ', o.note);
await (o.letter = 123);
console.log('awaited o.letter : ', await o.letter);
console.log('o.letter = 321 : ', o.letter = 321)
console.log('o.note : ', o.note);
console.log('await (o.letter = "awaited letter"): ', await (o.letter = 'awaited letter'));
console.log('o.note : ', o.note);
console.log('await o.letter : ', await o.letter);
setTimeout(() => {
console.log('o.note : ', o.note);
}, 200);
setTimeout(async () => {
console.log('as_async_value_set await', await (o.as_async_value = async () => { return 'as_async_value await'}));
console.log('as_async_value_set await o.note : ', o.note);
}, 300);
setTimeout(async () => {
console.log('as_async_value_set', o.as_async_value = async () => { return 'as_async_value'});
console.log('as_async_value_set o.note : ', o.note);
}, 350);
setTimeout(() => {
console.log('as_async_value_set after timeout o.note : ', o.note);
}, 400);
setTimeout(async () => {
console.log('as_async_value_set non async', o.as_async_value = 'non_async_as_async_value_value');
console.log('as_async_value_set non async o.note : ', o.note);
}, 500);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment