PubSub.js multiple subscriptions, or a different way to handle awaiting on multiple callbacks
<p>I am trying to figure out the best way to handle this scenario. Basically I want the flow to work like this:</p>

<p>1.) Get configuration data from server (async)</p>

<p>2.) Run doStuff() after configuration data is received (async)</p>

<p>3.) Run postResults after doStuff() completes</p>

<p>Currently I seem to have this flow working using PubSub.js, however I am trying to figure out how I can provide the results from config data (#1) to postResults (#3). While I seem to have the flow working with PubSub, I'm not sure how to access the configuration (#1) callback data from postResults (#3)</p>

<p>Here is a code summary:</p>

<pre><code>PubSub.subscribe('config', doStuff());
fetchConfigurations();

fuction fetchConfigurations () {
    var req = new XMLHttpRequest();
    var url = CONFIGURATION_SERVER_URL;
    req.onreadystatechange = function() {
        if (req.readyState == 4 &amp;&amp; req.status == 200) {
            var configObject = eval('(' + req.responseText + ')');
            PubSub.publish('config', configObject);
        } else {
            console.log("Requesting config from server: " + url);
        }
    }
    req.open("GET", url, true);
    req.send(null);
}


function doStuff() {
    PubSub.subscribe('results', postResults);
    var results = {};   

        // do some async work...

    results['test1'] = "some message";
        results['test2'] = "another message";
        PubSub.publish('doStuff', results);
}

function postResults (doStuffId, doStuffData) {
    var req = new XMLHttpRequest();
    var url = TEST_RESULTS_URL; // I want to get this from the configObject is get in fetchConfigurations
    req.open("POST",url,true);
    req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    req.send(doStuffData['test1'] + doStuffData['test2']);
}
</code></pre>
