Asynchronous wait for an condition to be met

asynchronous, node.js

Solution

I generally like to use the async library for doing things like this, but since you don't want to be referred to external libraries for this. I will write a simple function that checks it at an interval to see if the variables have been set.

Interval Check Method

var _flagCheck = setInterval(function() {
    if (flag1 === true && flag2 === true && flag3 === true) {
        clearInterval(_flagCheck);
        theCallback(); // the function to run once all flags are true
    }
}, 100); // interval set at 100 milliseconds

Async Library Parallel Method

var async = require('async');

async.parallel([
    function(callback) {
        // handle flag 1 processing
        callback(null);
    },

    function(callback) {
        // handle flag 2 processing
        callback(null);
    },

    function(callback) {
        // handle flag 3 processing
        callback(null);
    },
], function(err) {
    // all tasks have completed, run any post-processing.
});

Problem

In my code I have three variables whose value depend on an external function call, they can be set to true in any moment and in any order, so, they are more like flags. I need a function to be only once these three variables are set to true. How can I perform a wait for these three variables to be true, without blocking my server, in an asynchronous way? (I do not want to be refered to external libraries)

Original source