0

I have already read these answers, but I wasn't able to solve:

Adapt old geo to D3 v5, how to express a queue of Promise?

d3.js v5 - Promise.all replaced d3.queue

My code in d3 v4 looks like

d3.queue()
  .defer(d3.json, "path/file.json")
  .defer(populate,map,data)
  .await(ready);
}
function populate(map,data,callback) {
  .. code ..
  callback(null);

}

function ready(error, topo) {
  .. code ..
    }

I would like to replace this with Promise

stackfac
  • 57
  • 8
  • As I told you in my other answer, `d3.queue` is completely unnecessary here. Just use `d3.json` and put your `populate` function inside the `then` method, after the data is fetched. – Gerardo Furtado Jul 19 '20 at 03:36

1 Answers1

0

I'm assuming your populate function returns topo. You can use promise chaining:

function populate(map, data) {
  .. code ..
  return // return topo?
}

function ready(topo) {
  // map
  .. code ..
}
    
Promise.all([d3.json("path/file.json")])
  .then(([data]) => populate(map, data))
  .then(topo => {
    ready(topo)
  })

Instead of doing continuation using a callback, you can return the value normally, and it'll be picked up by the next function in the chain.

xy2
  • 6,040
  • 3
  • 14
  • 34
  • My populate function is not returning anything! Like this tutorial [link](https://www.d3-graph-gallery.com/graph/choropleth_basic.html) – stackfac Jul 17 '20 at 13:08
  • 1
    It doesn't matter if a function returns something or nothing, the flow remains the same. Simply omit the handling of data, and the same Promise.all pattern is valid. You can remove the null callback entirely, and just return null from the populate function, as the answer suggests. – cmonkey Jul 17 '20 at 15:04