Y
Y
yurygolikov2017-07-19 22:50:00
JavaScript
yurygolikov, 2017-07-19 22:50:00

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

I also tried to make the set handler synchronous, and assign Promise in target[prop]. Anyway, the assignment itself in the handler happens later.
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);

I'm using the latest version of node.js. Using the new async/await syntax

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nicholas, 2017-07-19
@healqq

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
    }

But there is no such syntax (and it seems not planned).
Why not hang Proxy on setValue itself?
const proxy = new Proxy(target, {
 async setValue(target, prop) {
    await PromiseTimeOut();
    target[prop] = value;
    return true;
  },
});

Then it will be possible to write
await proxy.setValue(1)

K
Kovalsky, 2017-07-20
@lazalu68

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.

Something like this
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"

As suggested to you, proxy.setValue can also be made an asynchronous function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question