Answer the question
In order to leave comments, you need to log in
How to work with asynchronous operations in the Proxy handler in ES6?
How to force the Proxy handler to wait after the monitored action (for example, wait for the Proxy handler of the attach operation, and only then continue execution)?
Most likely, I do not understand or missed something. Thanks for answers.
Simplified example:
function PromiseTimeOut() {
return new Promise((resolve, reject) => {
setTimeout(resolve ,1000);
});
}
const target = {
value: null,
setValue(value) {
this.value = value;
console.log(this.value); //Тут будет NULL
}
};
const proxy = new Proxy(target, {
get(target, prop) {
return target[prop];
},
async set(target, prop, value) {
await PromiseTimeOut();
target[prop] = value;
return true;
}
});
proxy.setValue(1);
//В этом месте исполнение должно подождать асинхронного обработчика Proxy.set
console.log(proxy.value);//И тут будет NULL
function PromiseTimeOut() {
return new Promise((resolve, reject) => {
setTimeout(resolve ,1000);
});
}
const target = {
value: null,
async setValue(value) {
this.value = value;
await this.value; // СЮДА НЕ ПРИХОДИТ ПРОКСИ, а по идее должен
console.log(this.value); //Тут будет NULL
}
};
const proxy = new Proxy(target, {
get(target, prop) {
return target[prop];
},
set(target, prop, value) {
target[prop] = new Promise(async (resolve, reject) => {
await PromiseTimeOut();
resolve();
});
return true;
}
});
proxy.setValue(1);
Answer the question
In order to leave comments, you need to log in
It seems to me that this won't work. Essentially you need to write something like:
setValue(value) {
await this.value = value;
console.log(this.value); //Тут будет NULL
}
const proxy = new Proxy(target, {
async setValue(target, prop) {
await PromiseTimeOut();
target[prop] = value;
return true;
},
});
await proxy.setValue(1)
I was talking about something like a separate procedure into which you can shove all the actions you need and there already stop anything and however you like.
function PromiseTimeOut(delay) {
return new Promise((resolve, reject) => {
setTimeout(resolve, delay);
});
}
var target = {
value: null,
setValue(value) {
this.value = value;
}
};
var proxy = new Proxy(target, {
get(target, prop) {
return target[prop];
},
set(target, prop, value) {
target[prop] = value;
return true;
}
});
async function demo() {
proxy.setValue('I have no idea what i\'m doing');
await PromiseTimeOut( 1000 );
console.log( proxy.value );
}
demo();
// Promise, а через секунду
// "I have no idea what i'm doing"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question