0

I know how promises and callbacks work in node js but I am very confused about how to capture custom events.
Let's say I have a global variable x

 var x = 10 

Some asynchronus functions in the event loop update value of x. How can I capture variable x having a particular value? Like what should I do when I want to run a function

    foo()

when variable x attain a value of let's say 50?

2
  • Can you tell how the value of x is being modified either by synchronous function or asynchronous function? Commented Mar 18, 2017 at 12:39
  • x is being modified asynchronously. Commented Mar 18, 2017 at 12:44

2 Answers 2

2

Use a function to set the variable:

var x = 10;

function setX(value) {
    x = value;

    if (x === 50) {
        foo();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

you can apply a check before calling a function like :

var num = 5;

function yourFunction(pass_value) {
    num  = num + pass_value; //your condition

   num == 50 && foo();    //it will only call when num = 50
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.