how to express a queue of Promise in d3 v5

我与影子孤独终老i 提交于 2020-07-23 06:42:44

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/62952888/how-to-express-a-queue-of-promise-in-d3-v5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!