d3-queue icon indicating copy to clipboard operation
d3-queue copied to clipboard

bind variable to .await()

Open timmy-guyon opened this issue 8 years ago • 2 comments

Is it possible to pass a variable to .await()? It doesn't look like .await() supports .bind(). Forgive my ignorance, I'm new to both d3 and asynchronous programming.

Here's my code for context `function update_left_chart_v2() { console.log("\n update left chart called v2!"); var leftSection = $("#dashboard > div:nth-child(2) > div.left-section").width();

for (svgSection in indexSVGSections) {
    //define variables for section
    sectionNumber = indexSVGSections[svgSection]["sectionNumber"];
    sectionMetric = indexSVGSections[svgSection]["sectionMetric"];
    sectionMetricCode = indexSVGSections[svgSection]["sectionMetricCode"];
    sectionIndexes = indexSVGSections[svgSection]["indexes"];
    console.log('section number: ' + sectionNumber + ' |section metric: ' + sectionMetric + '|section metric code: ' + sectionMetricCode + '\n');



    //add each d3.csv call to a queue, process when all completed
    var q = d3.queue();

    for (var index in sectionIndexes) {
        index = sectionIndexes[index];
        var fileName = index["fileName"];
        var indexActive = index["active"];
        var className = index["className"];

        if (indexActive) {
            console.log(index);
            var filePath = directory + "out-" + fileName + "-" + sectionMetricCode + ".csv";
            console.log(filePath);

            q.defer(d3.csv, filePath);

        } else {

            q.defer(nullIndex);
        }

    }

    function nullIndex(callback) {
        callback(null);
    }
    

    q.await(function(error, index1Data, index2Data, index3Data) {
       //THIS IS WHERE I NEED TO USE THE VARIABLE "sectionNumber" which changes with each iteration of for loop
       //
       console.log(sectionNumber);
        if (error) {
            console.log(error);
        } else {
            if(sectionCounter > 5){
                sectionCounter = 1;
            }

            console.log("await finished");
            console.log(" index 1 data");
            console.log(index1Data);

            // console.log("index 2 data");
            // console.log(index2Data);
            // console.log("\n");

            // console.log("index 3 data");
            // console.log(index3Data);
            // console.log("\n");

            var dataAssigned = false;
            index1Data.forEach(function(rowData, i) {
                rowDate = rowData.date;
                rowValue = rowData.value;
                if (rowDate.substr(0, 4) == sliderYear && !dataAssigned) {
                    console.log("date: ", rowDate, " | row value: ", rowValue, " | i: ", i);
                    dataAssigned = true;

                    // indexSVGSections[section1]

                }

            });
            console.log("\n");

        }
    });

}

}`

when "console.log(sectionNumber)" is called, it reads and calls it as its last assigned value, as it's being called asynchronusly. I need to bind each iteration of sectionNumber to .await() so it can read it as it changes.

timmy-guyon avatar Nov 02 '17 14:11 timmy-guyon

Just a suggestion - you could wrap d3.csv in a custom function and then pass sectionNumber to this function.

The q.defer call might look like this -

q.defer(myCustomCsvLoader, filePath, sectionNumber)

git-ashish avatar Nov 03 '17 14:11 git-ashish

Thanks! I ended up calling defer.(sectionNumber). The result was that sectionNumber was passed to .awaitAll() as the first element in the returned array. Thanks for your suggestion!

On Nov 3, 2017, at 10:44 AM, Ashish Singh [email protected] wrote:

Just a suggestion - you could wrap d3.csv in a custom function and then pass sectionNumber to this function.

The q.defer call might look like this -

q.defer(myCustomCsvLoader, filePath, sectionNumber)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/d3/d3-queue/issues/70#issuecomment-341723583, or mute the thread https://github.com/notifications/unsubscribe-auth/AOe4e__cNNTbV0hbqbBbq0XC2xPXZoxhks5syybIgaJpZM4QP0kH.

timmy-guyon avatar Nov 03 '17 15:11 timmy-guyon