Reputation: 63191
Consider the following snippet - which attempts to track the progress of data operations occurring through d3.queue().defer()
:
var data = [];
function analyze(error, f ) {
if (error) {
console.log(error);
}
console.log('Inside analyze: f.length=' + f.length);
data.push(f);
}
var q = d3.queue()
.defer(d3.csv, fnames[0])
.await(analyze);
console.log('after queue: data.len=' + data.length);
function updateViz(data2) {
// Do some d3 stuff with data2.. takes some time..
// ..
console.log('Finished updateViz: data2.len=' + data2.length);
}
updateViz(data);
console.log('after updateViz: len=' + data.length);
The output - as vieweed in the chrome
javascript console is:
So then: the data is appearing only after a moderately lengthy operation (udpateViz()
).
The issue is: I need the data available at the beginning of that method. Also more generally: what can be done to have a deterministic completion of the queue().defer()
- so that subsequent operations may be able to depend on the data being present?
Upvotes: 1
Views: 1585
Reputation: 555
d3.queue is an asynchronous function meaning that execution of further code (as calling of updateViz) does not wait for it to complete. The easiest solution is to put the code that depends on completion of d3.queue into its callback function. The callback function of d3.queue is always executed after all the deferred steps are completed.
var q = d3.queue()
.defer(d3.csv, fnames[0])
.await(updateViz);
function updateViz(error, data2) {
if (error) {
console.log(error);
}
updateViz(data2);
}
Upvotes: 2