javascript - Sync variables / Create an event listener for variables change value? -
suppose have 2 variables:
var connection1, connection2; connection1 = connection2 = false;
and have 2 different functions connect remote node , change each variable true
upon connection:
node1.on('open', function () { connection1 = true; }); node2.on('open', function () { connection2 = true; });
i want function run after both connections made, without checking other variable inside each on
function.
how can create event listener trigger when both values set true
without using setinterval
?
promise there save day.
// promise.all fulfill when promises resolved or 1 rejected promise.all( [ new promise( function(resolve, reject) { node1.on("open", resolve); } ), new promise( function(resolve, reject) { node2.on("open", resolve); } ) ] ) .then( function() { console.log("connections 1 & 2 opened"); } ) .catch( function(error) { console.log("error:", error); } );
Comments
Post a Comment