Reputation: 431
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?
Upvotes: 0
Views: 60
Reputation: 1104
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
}
Upvotes: 0
Reputation: 21708
Use a function to set the variable:
var x = 10;
function setX(value) {
x = value;
if (x === 50) {
foo();
}
}
Upvotes: 2