-2

For example in JavaScript, I want to watch a variable and block the control until the variable is changed. I could do something like this:

var foo = 1;
// Some async function that will modify `foo`
while (foo === 1){}
//This code will run after `foo`  changes
console.log("New value of foo is", foo);

But is this the right way to achieve that? Is it too hard on CPU?

EDIT: Basically what I want to achieve is, make the code look like it's sync even though there is an async function.

EDIT2: My question isn't duplicate because of the reason mentioned in the edit above.

snehanshu.js
  • 137
  • 1
  • 12
  • 1
    This is definitely not the right way, as it locks the UI. What does the async function look like? You should use the features of that function, not a `while` loop. – Sebastian Simon Jul 11 '18 at 18:04
  • Yes its not the correct way. It will use your CPU regularly – Rajat Jain Jul 11 '18 at 18:04
  • 2
    Not only is it too hard on the CPU, it won't even work, so no, don't do this. – JLRishe Jul 11 '18 at 18:05
  • 1
    Yes. Tight spinning is usually an indication that you're doing things wrong. There are so many different ways that this kind of problem has been addressed, so with no context, this question is too broad. – spender Jul 11 '18 at 18:05
  • Folks, I edited the question. Have a look now :) – snehanshu.js Jul 11 '18 at 18:11
  • 2
    Regarding your edit: see [`async`/`await`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) and [`Promises`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises). – Sebastian Simon Jul 11 '18 at 18:12
  • Possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Heretic Monkey Jul 11 '18 at 18:13
  • @Xufox `await` is good, but it's still asynchronous from outside because we have to add the `async` keyword on the outer function. I want to achieve complete synchronity like in Python. – snehanshu.js Jul 11 '18 at 18:19
  • A `while (foo === 1){}` will result in an endless blocking loop, which won't allow any other code to change `foo`, no matter if it is in an async function or not. There is not way to make this work. An async function does not create a new thread, it just _"splits"_ the function a the parts where an `await` appears to allow other code to interleave. – t.niese Jul 11 '18 at 18:28
  • @t.niese What if I call the async function just before the `while` loop? – snehanshu.js Jul 12 '18 at 06:38
  • Calling an asynchronous function does not mean that the code contained in it is executed in parallel or that the executed code is necessarily asynchronous ([demo 1](https://jsfiddle.net/kwjdycr4/)). Getting the result is async. If you have an `await` in the async function then everything up to the await statement (including that function call) is directly executed ([demo 2](https://jsfiddle.net/kwjdycr4/5/)) and the code after the await is postponed until the current code flow is finished, and the code after the event is executed. – t.niese Jul 12 '18 at 08:24
  • So either `while (foo === 1){}` is superfluous because `foo` was change to something different to `1` by the function that was called before and the loop never starts. Or `while (foo === 1){}` will block forever because no other code can run while `while (foo === 1){}` is running. – t.niese Jul 12 '18 at 08:26

1 Answers1

-3

There's an object watch. So if you want to watch an object, do the following:

var obj = {foo: 1}
obj.watch('foo', (id, oldval, newval) => {
    //do something
})

You won't be watching a naked variable, but it's not a huge deal. You could make an object that just contains the values you want to watch and use appropriately.

Eric Yang
  • 2,678
  • 1
  • 12
  • 18
  • 2
    No, **do not use this**! [`Object.prototype.watch`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/watch) is _obsolete, non-standard and deprecated_. The OP talks about _asynchronous functions_. These have plenty of methods to solve this. Not even Proxies are needed here. There’s no need to solve problems with the wrong tools. – Sebastian Simon Jul 11 '18 at 18:08
  • Instead of `watch` use Proxies https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy – pmkro Jul 11 '18 at 18:08