I want to chain JS promise to D3 v5 chart building, and want to resolve it after the animation, but it's not working, the code below also show different types of attempts in the comments, none of them is working, option 3 cause exception (Uncaught (in promise) TypeError: Cannot read property 'on' of undefined), option 2 and option 1 just fail silently. Please advise, thanks heaps! PS: dataset TrumpStatsFiltered is correct, as I tested the code without the promise.
var dur = 400;
var t = d3.transition().duration(dur);
var durR = 400;
var tR = d3.transition().duration(durR).ease(d3.easeBounce).delay(220);
var swingState1 = d3.select('#svgSwingStateGraph1');
var swingState1Promise = () => {
return new Promise((resolve, reject) => {
swingState1
.selectAll('text')
.data([TrumpStatsFiltered], d => d.swing_state_1)
.join(
enter => {
enter
.append('text')
.attr('x', -150)
.attr('y', 30)
.text(d => d.swing_state_1 == 'Trump' ? "Trump":"Hillary")
.attr("stroke", d => d.swing_state_1 == 'Trump' ? "green":"orange")
.transition(t)
.attr('x', 75)
//.on("end", response => resolve(response)) //- option 2
},
update => {
update
.transition(t)
.text(d => d.swing_state_1 == 'Trump' ? "Trump":"Hillary")
.attr("stroke", d => d.swing_state_1 == 'Trump' ? "green":"orange")
//.on("end", response => resolve(response)) //- option 2
},
exit => {
exit
.transition(tR)
.attr("stroke", "tomato")
.attr('x', 200)
.remove()
//.on("end", response => resolve(response)) //- option 2
}
)//.on("end", response => resolve(response)) //- option 3
swingState1.on("end", response => resolve(response)) // option 1
})
}
Promise.all([swingState1Promise()]).then(() => {
***I need to hit this point!***
});